-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.js
44 lines (38 loc) · 1.35 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
const express = require("express");
const cors = require("cors");
const commentRoutes = require("./routes/comments");
const organizationsRoutes = require("./routes/organizations");
const applicationsRoutes = require("./routes/applications");
const documentationRoutes = require("./routes/documentation");
const adminsRoutes = require("./routes/admins");
const msAdminsRoutes = require("./routes/msadmins");
const CustomError = require("./utils/customError");
const errorHandler = require("./utils/errorhandler");
const globalRateLimiter = require("./middleware/globalRateLimiter");
require("dotenv").config();
const app = express();
// setup middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
//setup app routes
app.use(globalRateLimiter);
app.use("/v1/comments", commentRoutes);
app.use("/v1/organizations", organizationsRoutes);
app.use("/v1/applications", applicationsRoutes);
app.use("/v1/admins", adminsRoutes);
app.use("/v1/msadmins", msAdminsRoutes);
app.use(["/v1", "/"], documentationRoutes);
// Invalid route error handler
app.use("*", (req, res, next) => {
const error = new CustomError(
404,
`Oops. The route ${req.method} ${req.originalUrl} is not recognised`
);
next(error);
});
// error handler
app.use((err, req, res, next) => {
errorHandler(err, req, res, next);
});
module.exports = app;