-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (44 loc) · 1.15 KB
/
Copy pathindex.js
File metadata and controls
52 lines (44 loc) · 1.15 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
47
48
49
50
51
52
import express from "express";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import jsonwebtoken from "jsonwebtoken";
import routes from "./src/routes/crmRoutes";
const app = express();
const PORT = 3000;
// mongoose connection
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/CRMdb", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// bodyparser setup
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.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}`));