-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (47 loc) · 1.48 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
/*all important dependencies*/
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('morgan');
const jwt = require('jsonwebtoken');
const pgp = require('pg-promise');
const path = require('path');
const cors = require('cors');
require('dotenv').config();
/*instance of express framework*/
const app = express();
/*initialize a connection between front and back-end*/
app.use(cors());
/*imply necessary dependencies for express*/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('./client/build'));
/*exprees middleware to check a valide token*/
app.use((req, res, next) => {
if(req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === 'JWT') {
jwt.verify(req.headers.authorization.split(' ')[1], process.env.SECRET, (err, decode) => {
if(err) {
req.user = undefined;
return res.json({message: 'Please login'});
}
req.user = decode;
console.log('go to next level');
next();
})
} else {
req.user = undefined;
next();
}
});
/*create port*/
const PORT = process.env.PORT || 3001;
/*start listening port*/
app.listen(PORT, () => {
console.log(`I am listening port ${PORT}`);
});
/*due to the following path redirect to routes*/
const missionRoutes = require('./routes/missionRoutes');
app.use('/api', missionRoutes);
app.get('*', (req, res) => {
res.status(404).send('Ooops!');
});