forked from Reddit-Replica/SW-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (98 loc) · 2.98 KB
/
app.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
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
107
108
109
110
111
112
113
114
115
116
import dotenv from "dotenv";
import express from "express";
import multer from "multer";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import mongoose from "mongoose";
import swaggerUI from "swagger-ui-express";
import swaggerJsDoc from "swagger-jsdoc";
import mainRouter from "./routes/routes.js";
import bodyParser from "body-parser";
import morgan from "morgan";
import { fileStorage, fileFilter } from "./utils/files.js";
import rateLimit from "express-rate-limit";
const app = express();
const REQUEST_LIMIT = parseInt(process.env.REQUEST_LIMIT) || 100;
dotenv.config();
app.use(bodyParser.json({ limit: "200mb" }));
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).fields([
{ name: "images", maxCount: 100 },
{ name: "video", maxCount: 1 },
{ name: "avatar", maxCount: 1 },
{ name: "banner", maxCount: 1 },
])
);
// That's morgan for tracking the api in the terminal
// Will be removed later
app.use(morgan("dev"));
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: REQUEST_LIMIT, // Limit each IP to REQUEST_LIMIT requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
// Apply the rate limiting middleware to all requests
app.use(limiter);
// Log stream for morgan to make the log file in the server
const accessLogStream = fs.createWriteStream(
path.join(__dirname, "logs/access.log"),
{
flags: "a",
}
);
app.use(
morgan("combined", {
stream: accessLogStream,
})
);
app.use("/images", express.static(path.join(__dirname, "images")));
app.use("/videos", express.static(path.join(__dirname, "videos")));
const port = process.env.PORT || 3000;
app.use((_req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"GET,POST,PUT,DELETE,PATCH,OPTIONS"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
next();
});
// app.use(cors());
let DB_URL;
// eslint-disable-next-line max-len
if (process.env.NODE_ENV.trim() === "testing") {
DB_URL = process.env.MONGO_URL_TESTING.trim();
} else {
DB_URL = process.env.MONGO_URL.trim();
}
mongoose
.connect(DB_URL, { useNewUrlParser: true })
.then(() => {
console.log("connected to mongo nnew version");
})
.catch((error) => {
console.log("unable to connect to mongoDB : ", error);
});
// swagger options
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Read-it",
version: "1.0.0",
description: "API-Documentation",
},
},
apis: ["./routes/*.js"],
};
const specs = swaggerJsDoc(options);
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(specs));
app.use(mainRouter);
app.listen(port, () => {
console.log(`Started on port ${port}`);
});
export default app;