-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (52 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require("dotenv").config({path:"./config.env"});
const express= require('express')
const app = express()
const cors =require('cors')
const mongoose = require('mongoose')
const User = require('./models/user.model')
const jwt = require('jsonwebtoken')
const path = require('path');
const PORT = process.env.PORT || 1337 ;
const DB= 'mongodb+srv://vishal:[email protected]/mernsignup?retryWrites=true&w=majority'
mongoose.connect(DB).then(()=>{
console.log(`connection successful`);
}).catch((err)=>console.log(`no connection`));
app.use(cors())
app.use(express.json())
app.post('/login', async (req, res)=>{
const user = await User.findOne({
email: req.body.email,
password: req.body.password,
})
if (user){
const token =jwt.sign({
name: user.name,
email: user.email,
}, 'blowglow1234')
return res.json({ status: 'ok', user:token})
}else{
return res.json({ status: 'error', user:false})
}
})
app.post('/register', async (req, res)=>{
console.log(req.body)
try{
await User.create({
name: req.body.name,
email: req.body.email,
password: req.body.password,
})
res.json({status: 'ok'})
} catch(err){
res.json({status:'error', error:'Duplicate email'})
}
})
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', "build", "index.html"));
});
}
app.listen(PORT, ()=>{
console.log(`Server started on sever ${PORT}`)
})