-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
78 lines (64 loc) · 1.99 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require("dotenv").config();
require("express-async-errors");
const express = require("express");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
// security packages
const helmet = require("helmet");
const cors = require("cors");
const xss = require("xss-clean");
const { rateLimit } = require("express-rate-limit");
// api documentation: swagger
const swaggerDocument = require("yamljs").load("./swagger.yaml");
const swaggerUi = require("swagger-ui-express");
// imports
const generateAdmins = require("./util/generateAdmins");
const authRoutes = require("./routes/authRoutes");
const userRoutes = require("./routes/userRoutes");
const adminRoutes = require("./routes/adminRoutes");
const mainRoutes = require("./routes/mainRoutes");
const historyRoutes = require("./routes/historyRoutes");
const { errorHandler, notFound } = require("./middleware/errorHandler");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.set("trust proxy", 1);
app.use(
rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100,
})
);
app.use(helmet());
app.use(
cors({
origin: "https://text-crypt.netlify.app",
optionsSuccessStatus: 200,
})
);
app.use(xss());
app.get("/", (req, res) => {
res.send(
'<h2>Welcome to Textual Cryptography API</h2><a href="/api-docs">Documentation</a>'
);
});
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/cryptography", mainRoutes);
app.use("/api/v1/admin", adminRoutes);
app.use("/api/v1/user", userRoutes);
app.use("/api/v1/history", historyRoutes);
app.use(notFound);
app.use(errorHandler);
const PORT = process.env.PORT || 4000;
const start = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI);
await generateAdmins();
app.listen(PORT, console.log(`server listening in port: ${PORT}`));
} catch (error) {
console.log(error);
}
};
start();