-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (34 loc) · 1.19 KB
/
server.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
require('dotenv').config();
const express = require('express');
const methodOverride = require('method-override');
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const routes = require('./routes');
const app = express();//app is an object
//middleware to convert request body into urlencoded oject
app.use(express.urlencoded({extended: true}));
app.use(methodOverride('_method'));
app.use(express.static("public"));
app.use(cookieParser());
const verifyToken = (req, res, next) => {
let token = req.cookies.jwt;
console.log(`Token: ${token}`);
jwt.verify(token, process.env.JWT_SECRET, (err, decodedUser) => {
if(err || !decodedUser){
return res.send('Error in JWT');
}
console.log(decodedUser)
req.user = decodedUser;
next();
})
}
app.use('/auth', routes.auth);
app.use('/fruits', verifyToken, routes.fruits);
app.use('/users', verifyToken, routes.users);//http:localhost:3000/users/1
app.get('/', (req, res) => {
res.render('users/index.ejs');
})
//listen is used to run your app on port 3000
app.listen(process.env.PORT, () => {
console.log(`I am listening on port ${process.env.PORT}`);
})