-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (41 loc) · 1.4 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
require("dotenv").config();
const Logger = require("./Library/Loggers/ServerLogger")
const morgan = require("morgan");
const {db_connect, db_close} = require("./Database/db_handler");
const morganMiddleware = morgan(
":method :url :status :res[content-length] - :response-time ms",
{
stream: {
// Configure Morgan to use our custom logger with the http severity
write: (message) => Logger.info(message.trim()),
},
}
);
const express = require("express");
const server = express();
const api_route = require("./Routes/ApiRouter");
const login_route = require("./Routes/LoginRouter");
const user_route = require("./Routes/UserRouter");
const ErrorHandler = require("./Middlewares/ErrorHandler");
const {authentication} = require("./Middlewares/Authentication");
const PORT = process.env.PORT || 3000;
server.use(morganMiddleware);
server.use(express.json());
server.use("/api", authentication, api_route);
server.use("/login", login_route);
server.use("/user", user_route);
server.use(ErrorHandler);
const start = async () => {
try {
await db_connect(process.env.DB_PROD_URL);
server.listen(PORT, () =>
Logger.info(`Server is up and running at port: ${PORT}`)
);
} catch (error) {
await db_close();
Logger.info("Something went wrong in server")
console.error(error);
process.exit(1);
}
};
start();