-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
47 lines (41 loc) · 1.25 KB
/
server.ts
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
import express from "express";
import { generateKeyPair } from "crypto";
const [basePort, count] = process.argv.slice(2);
const apps = new Array(Number(count)).fill(null).map(($) => express());
const heavyComputation = async (): Promise<any> => {
return new Promise((resolve) => {
generateKeyPair(
"rsa",
{
modulusLength: 1024,
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
},
},
(err: Error | null, publicKey: string, privateKey: string) => {
if (err) return err;
resolve(publicKey + privateKey);
}
);
});
};
const handler =
(num: number) => async (req: express.Request, res: express.Response) => {
const { method, url, headers, body } = req;
const ress = await heavyComputation();
res.json({
server: num,
data: ress,
}).status(200);
};
apps.forEach((app, idx) => {
app.get("*", handler(idx));
});
apps.forEach((app, idx) => {
app.listen(Number(basePort) + idx);
});