-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressServer.js
More file actions
45 lines (34 loc) · 1.38 KB
/
expressServer.js
File metadata and controls
45 lines (34 loc) · 1.38 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
const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const passport = require("passport");
require("./passport/passportStrategies");
const config = require("./configs/mainConfigs");
const customErrorHandler = require("./errorHandlers/middlewares/customErrorHandler");
const defaultErrorHandler = require("./errorHandlers/middlewares/defaultErrorHandler");
const rootRouter = require("./routers/rootRouter/rootRouter");
const authRouter = require("./routers/authRouter/authRouter");
const jsonRouter = require("./routers/jsonRouter/jsonRouter");
const imageRouter = require("./routers/imageRouter/imageRouter");
const app = express();
// Hiding morgon loggin outputs in test enviroment to avoid clutter.
if (config.NODE_ENV !== "test") {
app.use(morgan("dev"));
}
app.use(cors()); // Enable all CORS requests from any origin.
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Public Endpoints
app.use("/", rootRouter);
app.use("/auth", authRouter);
// Protected Endpoints
app.use("/json", jsonRouter);
app.use("/image", imageRouter);
// Error Handlers
app.use(customErrorHandler);
app.use(defaultErrorHandler);
// Path not found is not a error. So we need custom middleware to catch them.
app.use((req, res) => {
res.status(404).json({ success: false, errMsg: "Requested Path Not Found." });
});
module.exports = app;