-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathworker.js
More file actions
36 lines (30 loc) · 860 Bytes
/
worker.js
File metadata and controls
36 lines (30 loc) · 860 Bytes
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
'use strict';
const http = require('node:http');
const BASE_PORT = 2000;
const pid = process.pid;
const id = parseInt(process.argv[2], 10);
const port = BASE_PORT + id - 1;
const user = { name: 'jura', age: 22 };
const routing = {
'/': 'welcome to homepage',
'/user': user,
'/user/name': () => user.name,
'/user/age': () => user.age,
};
const types = {
object: (o) => JSON.stringify(o),
string: (s) => s,
number: (n) => n.toString(),
undefined: () => 'not found',
function: (fn, par, client) => JSON.stringify(fn(client, par)),
};
console.log(`Worker: ${id}, pid: ${pid}, port: ${port}`);
http
.createServer((req, res) => {
const data = routing[req.url];
const type = typeof data;
const serializer = types[type];
res.setHeader('Process-Id', process.pid);
res.end(serializer(data, req, res));
})
.listen(port);