-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (49 loc) · 1.24 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
import express from "express";
import config from "./src/db/config.js";
import jwt from "jsonwebtoken";
import cors from "cors";
import bodyParser from "body-parser";
const app = express();
import user from "./src/routes/userRoute.js";
import comments from "./src/routes/commentsRoute.js";
import tasks from "./src/routes/tasksRoutes.js";
import notifications from "./src/routes/notificationsRoute.js";
//built inmiddleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// JWT middleware
app.use((req, res, next) => {
if (
req.headers &&
req.headers.authorization &&
req.headers.authorization.split(" ")[0] === "JWT"
) {
jwt.verify(
req.headers.authorization.split(" ")[1],
config.jwt_secret,
(err, decode) => {
if (err) req.user = undefined;
req.user = decode;
next();
}
);
} else {
req.user = undefined;
next();
}
});
// routes access app
user(app);
comments(app);
tasks(app);
notifications(app);
app.get("/", (req, res) => {
res.send("Welcome to TaskMinder API");
});
//port
app.listen(config.port || 5000, () => {
console.log(`Server running`);
});