/
index.js
71 lines (58 loc) · 2.33 KB
/
index.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
const cluster = require("cluster");
const http = require("http");
const CONSTANTS = require("./constants.js");
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
const cpuCount = require("os").cpus().length;
let i = 0;
// Create a worker for each CPU
for (i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on("exit", (worker) => {
// Replace the dead worker
console.log(`Worker ${worker.id} died :(`);
cluster.fork();
});
} else {
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const CONSTANTS = require("./constants.js");
const app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// For CORS
app.use( (req, res, next) => {
// Website you wish to allow to connect
const origin = req.headers.origin;
if (CONSTANTS.ALLOWED_ORIGINS.indexOf(origin) > -1){
res.setHeader("Access-Control-Allow-Origin", origin);
}
//res.setHeader("Access-Control-Allow-Origin", CONSTANTS.WEB_SERVICE_URI);
// Request methods you wish to allow
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
// Request headers you wish to allow
res.setHeader("Access-Control-Allow-Headers", "Authorization,X-Requested-With,content-type,access-control-allow-methods,access-control-allow-origin");
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader("Access-Control-Allow-Credentials", true);
// Pass to next layer of middleware
next();
});
app.use("/", express.static(path.join(__dirname, "public")));
app.use("/dist", express.static(path.join(__dirname, "dist")));
app.use("/test", express.static(path.join(__dirname, "test")));
app.use("/vendor", express.static(path.join(__dirname, "node_modules")));
app.use("/docs", express.static(path.join(__dirname, "jsdoc")));
/* REST API */
app.get("/about", (req, res) => {
res.status(200).send(CONSTANTS.ABOUT);
});
app.listen(CONSTANTS.PORT, () => {
console.log(`${CONSTANTS.ABOUT} listening at port ${CONSTANTS.PORT}`);
});
//end cluster worker
};