Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (90 loc) · 2.46 KB
/
Copy pathserver.js
File metadata and controls
106 lines (90 loc) · 2.46 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import dotenv from "dotenv";
dotenv.config();
import async_error from "express-async-errors";
import cookieParser from "cookie-parser";
import express from "express";
import expressWinston from "express-winston";
// security
import helmet from "helmet";
import cors from "cors";
import xss from "xss-clean";
import rateLimiter from "express-rate-limit";
// swagger
import swaggerUI from "swagger-ui-express";
import YAML from "yamljs";
const swaggerDocument = YAML.load("./swagger.yaml");
import { app, server } from "./backend/socket/socket.js";
import authRouter from "./backend/routes/auth.js";
import itemsRouter from "./backend/routes/items.js";
import bidsRouter from "./backend/routes/bids.js";
import notificationsRouter from "./backend/routes/notifications.js";
import { notFoundMiddleware } from "./backend/middlewares/not-found.js";
import { errorHandlerMiddleware } from "./backend/middlewares/error-handler.js";
import { logger, requestLogger } from "./backend/logger/logger.js";
import pool from "./backend/db/dbConfig.js";
app.set("trust proxy", 1);
app.use(
rateLimiter({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100,
})
);
app.use(cookieParser());
app.use(express.json());
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
"'unsafe-inline'",
"'unsafe-eval'",
"http://cdnjs.cloudflare.com",
"https://cdn.socket.io",
],
objectSrc: ["'none'"],
},
},
})
);
app.use(cors());
pool.connect();
app.use(xss());
// logger
app.use(
expressWinston.logger({
winstonInstance: requestLogger,
statusLevels: true,
})
);
app.get("/", (req, res) => {
res.send(
"<h1>Welcome to Bidding App API</h1> <a href='/api-docs'>Documentation</a>"
);
});
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swaggerDocument));
app.use("/api/v1/users", authRouter);
app.use("/api/v1/items", itemsRouter);
app.use("/api/v1/items", bidsRouter);
app.use("/api/v1/notifications", notificationsRouter);
app.use(
expressWinston.errorLogger({
winstonInstance: logger,
})
);
// error handlers;
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const port = process.env.PORT || 3000;
const start = () => {
try {
server.listen(port, () => {
console.log(`Server is listenting on port ${port}`);
});
} catch (error) {
console.log("start error", error);
}
};
start();
export { app, server };