-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (46 loc) · 1.97 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
const cluster = require("cluster");
const CPUs = require("os").cpus().length;
const logger = require("./server/utility/logger");
const constants = require("./server/utility/constants");
if (cluster.isMaster) {
for (var i = 0; i < CPUs; i++) {
cluster.fork();
}
Object.keys(cluster.workers).forEach(function (id) {
logger.info("worker id : " + cluster.workers[id].process.pid + " up");
});
cluster.on("exit", function (worker, code, signal) {
logger.info('worker id : ' + worker.process.pid + " died");
});
} else {
const express = require("express");
const bodyParser = require("body-parser");
require("dotenv").config({ encoding: "utf8" });
const app = express();
const uuid = require("shortid")
const PORT = process.env.PORT;
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE");
res.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
res.setHeader("Cache-Control", "no-cache");
res.setHeader(constants.corr_id, req.get(constants.corr_id) ? req.get(constants.corr_id) : uuid.generate());
next();
})
require("./server/core/routes/index")(app);
app.use(function (req, res, next) {
let err = new Error(constants.not_found_message);
err.status = 404;
next(err);
});
app.use((error, req, res, next) => {
res.status(error.status || 500).jsonp({ message: error.message });
});
app.listen(PORT, () => {
logger.info(`Server is running on port ${PORT}`);
})
module.exports = app;
}