forked from speichs/Q2Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (47 loc) · 1.16 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
49
50
51
52
53
54
55
56
57
58
'use strict';
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
const port = process.env.PORT || 8000;
const notes = require('./routes/notes');
const users = require('./routes/users');
const videos = require('./routes/videos');
const tokens = require('./routes/tokens');
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static('public'));
app.use(tokens);
app.use((req, res, next) => {
const token = req.cookies.token;
if (token) {
jwt.verify(token, process.env.SECRET, (error, decoded) => {
if (error) {
res.clearCookie('token');
return next(error);
}
req.user = decoded;
next();
});
} else {
next();
}
});
app.use(users);
app.use(notes);
app.use(videos);
app.use((req, res) => {
res.sendStatus(404);
});
app.use((err, req, res, next) => {
console.error(err);
res.sendStatus(500);
});
app.listen(port, () => {
console.log('Listening on port ' + port);
});
module.exports = app;