-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (30 loc) · 858 Bytes
/
index.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
import express from "express";
import { deleteOrder } from "./controllers/deleteOrder.js";
import { IndexRoute } from "./controllers/index.js";
import { usersRoutes } from "./controllers/users/index.js";
import jwt from "jsonwebtoken";
import { jwtKey } from "./constrants.js";
import cors from "cors";
// Приложение
const app = express();
const port = 5000;
app.use(cors());
app.use((req, res, next) => {
const paths = ["/users/register", "/users/auth"];
const token = req.query.token || "";
if (paths.includes(req["_parsedUrl"].pathname)) {
next();
} else {
try {
const decoded = jwt.verify(token, jwtKey);
app.set("userId", decoded.id);
next();
} catch (e) {
res.status(403).send({ error: "not authorized" });
}
}
});
IndexRoute(app);
deleteOrder(app);
usersRoutes(app);
app.listen(port);