-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (49 loc) · 2.18 KB
/
server.js
File metadata and controls
78 lines (49 loc) · 2.18 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
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
// importing required packages and modules
const cors = require(`cors`);
const express = require(`express`);
const morgan = require(`morgan`);
const { logSuccess, logInfo, logWarning, logError } = require(`./dependencies/helpers/console.helpers`);
const { connectDatabase, disconnectDatabase } = require(`./dependencies/helpers/database.helpers`);
// importing required config params
const { server_MODE, NODE_PORT, MAX_FILE_SIZE_ALLOWED_BYTES, HTTP_STATUS_CODES: { SUCCESS } } = require(`./dependencies/config`);
// importing required routers
const { systemRoleRouter } = require(`./api/routers/system-role.router`);
// REMIANING ROUTERS GO HERE
// creating an instance of express server
const server = express();
// initializing server
(async () => {
try {
// Listening requests on the specified PORT
server.listen(NODE_PORT, logInfo(`Initializing server in ${server_MODE} mode. Please wait.`));
// declaring globals
global.CONNECTED_CLIENTS = {};
// 1-> middleware for handling cors
// 2-> middleware to log requests to the console
// 3-> middleware to parse json request body
// 4-> middleware to parse urlencoded request data
server.use(cors());
server.use(morgan(`dev`));
server.use(express.json({ limit: MAX_FILE_SIZE_ALLOWED_BYTES, verify: (req, res, buf) => req.rawBody = buf }));
server.use(express.urlencoded({ extended: false }));
// api handlers
server.use(`/api/system-roles`, systemRoleRouter); // validation done
// LIST OF API HANDLERS GO HERE
// creating test route
server.get(`/`, (req, res, next) => res.status(SUCCESS).send(`|| Service is UP & RUNNING in ${server_MODE} mode ||`));
// making connection to database
await connectDatabase();
// logging message to the console
logInfo(`Server is running @ port ${NODE_PORT}.`);
} catch (error) {
// this code runs in case of an ERROR @ runtime
// logging error message to the console
logError(`ERROR @ Server Initialization Process.`, error);
}
})();
// disconnecting from the database instance before killing
// the process
process.on(`SIGINT`, async () => {
// disconnecting server from database
await disconnectDatabase();
});