-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (34 loc) · 1.11 KB
/
index.js
File metadata and controls
42 lines (34 loc) · 1.11 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
import express from 'express';
import mongoose from 'mongoose';
import routes from './src/routes/crmRoutes.js'; // Add .js extension
import jsonwebtoken from 'jsonwebtoken';
const app = express();
const PORT = 3000;
// mongoose connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/CRMdb');
// body parsing middleware (built into Express 5.x)
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// 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 is running on port ${PORT}`)
);
app.listen(PORT, () =>
console.log(`your server is running on port ${PORT}`)
);