-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (37 loc) · 1.1 KB
/
Copy pathindex.js
File metadata and controls
46 lines (37 loc) · 1.1 KB
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
import express from 'express';
import routes from './src/routes/crmRoutes';
import mongoose from 'mongoose';
import jsonwebtoken from 'jsonwebtoken';
const app = express();
const PORT = 4000;
// Mongoose connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/CRMdb', {
useNewUrlParser: true,
useUnifiedTopology: true
});
// bodyparser setup
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//JWT setup
app.use((req, res, next) => {
if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === 'JWT') {
jsonwebtoken.verify(req.headers.authorization.split(' ')[1], 'RESTFULAPIs', (err, decode) => {
if (err) req.user = undefined;
req.user = decode;
next()
});
} else {
req.user = undefined;
next();
}
})
routes(app);
// serving static files
app.use(express.static('public'));
app.get('/', (req, res) =>
res.send(`Node and express server running on port ${PORT}`)
)
app.listen(PORT, () =>
console.log(`Your server is running on port ${PORT}`)
)