-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcluster.js
52 lines (43 loc) · 1.08 KB
/
cluster.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
const express = require("express");
const cors = require("cors");
const config = require("./config");
const axios = require("axios").default;
function init() {
let extension;
const _config = config();
try {
extension = require(`${_config.path}/extensions/cluster.js`);
} catch (err) {
console.error("Missing cluster extension");
process.exit(1);
}
const cluster = express();
cluster.use(express.json());
cluster.use(cors());
cluster.all("*", async (req, res) => {
const {
error = true,
https,
ip,
port,
path,
headers = {},
} = extension.apply(req);
if (error) {
return res.status(403).end();
}
try {
const response = await axios({
method: req.method,
url: `${https ? "https" : "http"}://${ip}:${port}${path}`,
headers,
data: req.method !== "GET" ? req.body : undefined,
});
res.status(response.status).json(response.data);
} catch (err) {
res.status(503).end();
}
});
cluster.listen(_config.port.cluster);
}
module.exports = { init };