-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
111 lines (83 loc) · 2.64 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
/**
* name : app.js.
* author : Aman Karki.
* created-date : 13-July-2020.
* Description : Root file.
*/
require("dotenv").config();
// express
const express = require("express");
const app = express();
// Health check
require("./healthCheck")(app);
// Setup application config, establish DB connections and set global constants.
global.config = require("./config/connections");
require("./config/globals")();
// Check if all environment variables are provided.
const environmentData = require("./envVariables")();
if (!environmentData.success) {
LOGGER.error("Server could not start . Not all environment variable is provided");
process.exit();
}
//required modules
const fileUpload = require("express-fileupload");
const bodyParser = require("body-parser");
const cors = require("cors");
const fs = require("fs");
const path = require("path");
const expressValidator = require('express-validator');
//To enable cors
app.use(cors());
app.use(expressValidator())
//health check
app.get(process.env.HEALTH_CHECK_URL, (req, res) => {
res.send("pong!");
});
app.use(fileUpload());
app.use(bodyParser.json({ limit: process.env.BODY_PARSER_LIMIT }));
app.use(bodyParser.urlencoded({
limit: process.env.BODY_PARSER_LIMIT,
extended: false
}));
app.use(express.static("public"));
fs.existsSync(process.env.LOGGER_DIRECTORY) ||
fs.mkdirSync(process.env.LOGGER_DIRECTORY);
//API documentation (apidoc)
if ( process.env.APPLICATION_ENV == "development" ) {
app.use(express.static("apidoc"));
app.get(process.env.APIDOC_URL, (req, res) => {
let urlArray = req.path.split("/");
urlArray.splice(0, 3);
let apidocPath = process.env.APIDOC_PATH + urlArray.join("/");
res.sendFile(path.join(__dirname, apidocPath));
});
}
app.all('*', (req, res, next) => {
if(ENABLE_FILE_LOGGING === "ON") {
LOGGER.info("Requests:", {
method: req.method,
url: req.url,
headers: req.headers,
body: req.body
})
}
if(process.env.ENABLE_CONSOLE_LOGGING === "ON") {
console.log("-------Request log starts here------------------");
console.log("Request URL: ", req.url);
console.log("Request Headers: ", req.headers);
console.log("Request Body: ", req.body);
console.log("Request Files: ", req.files);
console.log("-------Request log ends here------------------");
}
next();
});
// Router module
const router = require("./routes");
//add routing
router(app);
//listen to given port
app.listen(process.env.APPLICATION_PORT, () => {
console.log("Environment : " + process.env.APPLICATION_ENV);
console.log("Application is running on the port : " + process.env.APPLICATION_PORT);
});
module.exports = app;