|
1 | 1 | const http = require("http");
|
2 | 2 |
|
3 |
| -const host = 'localhost'; |
| 3 | +const host = "localhost"; |
4 | 4 |
|
5 | 5 | function requestListener(req, res) {
|
6 |
| - var body = ""; |
7 |
| - req.on('data', function (chunk) { |
8 |
| - body += chunk; |
9 |
| - }); |
10 |
| - req.on('end', function () { |
11 |
| - const payload = JSON.parse(body); |
12 |
| - res.writeHead(200); |
13 |
| - res.end(payload.value.toString()); |
14 |
| - }); |
15 |
| -}; |
| 6 | + var body = ""; |
| 7 | + req.on("data", function (chunk) { |
| 8 | + body += chunk; |
| 9 | + }); |
| 10 | + req.on("end", function () { |
| 11 | + const payload = JSON.parse(body); |
| 12 | + res.writeHead(200); |
| 13 | + res.end(payload.value.toString()); |
| 14 | + }); |
| 15 | +} |
16 | 16 |
|
17 | 17 | function send(api, value) {
|
18 |
| - return new Promise(function (onFulfilled, onRejected) { |
19 |
| - const req = http.request(api, { |
20 |
| - method: 'POST', |
21 |
| - headers: { |
22 |
| - 'content-type': 'application/json', |
23 |
| - }, |
24 |
| - }, res => { |
25 |
| - var content = ""; |
26 |
| - res.on('data', (chunk) => { |
27 |
| - content += chunk; |
28 |
| - }); |
29 |
| - res.on('end', () => { |
30 |
| - onFulfilled(+content); |
31 |
| - }); |
32 |
| - }); |
33 |
| - req.on('error', (err) => { |
34 |
| - onRejected(err); |
35 |
| - }); |
36 |
| - req.write(JSON.stringify({ "value": value })); |
37 |
| - req.end(); |
38 |
| - }); |
| 18 | + return new Promise(function (onFulfilled, onRejected) { |
| 19 | + const req = http.request( |
| 20 | + api, |
| 21 | + { |
| 22 | + method: "POST", |
| 23 | + headers: { |
| 24 | + "content-type": "application/json", |
| 25 | + }, |
| 26 | + }, |
| 27 | + (res) => { |
| 28 | + var content = ""; |
| 29 | + res.on("data", (chunk) => { |
| 30 | + content += chunk; |
| 31 | + }); |
| 32 | + res.on("end", () => { |
| 33 | + onFulfilled(+content); |
| 34 | + }); |
| 35 | + }, |
| 36 | + ); |
| 37 | + req.on("error", (err) => { |
| 38 | + onRejected(err); |
| 39 | + }); |
| 40 | + req.write(JSON.stringify({ value: value })); |
| 41 | + req.end(); |
| 42 | + }); |
39 | 43 | }
|
40 | 44 |
|
41 | 45 | async function sendAsync(api, value) {
|
42 |
| - while (true) { |
43 |
| - try { |
44 |
| - return await send(api, value); |
45 |
| - } catch (e) { } |
46 |
| - } |
| 46 | + while (true) { |
| 47 | + try { |
| 48 | + return await send(api, value); |
| 49 | + } catch (e) {} |
| 50 | + } |
47 | 51 | }
|
48 | 52 |
|
49 | 53 | async function calculateSum(port, n) {
|
50 |
| - var api = `http://${host}:${port}/`; |
51 |
| - var sum = +0; |
52 |
| - var tasks = []; |
53 |
| - for (var i = 1; i <= n; i++) { |
54 |
| - tasks.push(sendAsync(api, i)); |
55 |
| - } |
56 |
| - for (var i = 0; i < n; i++) { |
57 |
| - sum += await tasks[i]; |
58 |
| - } |
59 |
| - console.log(sum); |
| 54 | + const api = `http://${host}:${port}/`; |
| 55 | + var sum = +0; |
| 56 | + var tasks = []; |
| 57 | + for (var i = 1; i <= n; i++) { |
| 58 | + tasks.push(sendAsync(api, i)); |
| 59 | + } |
| 60 | + for (var i = 0; i < n; i++) { |
| 61 | + sum += await tasks[i]; |
| 62 | + } |
| 63 | + console.log(sum); |
60 | 64 | }
|
61 | 65 |
|
62 | 66 | function main() {
|
63 |
| - const args = process.argv.slice(2) |
64 |
| - const n = +args[0] || 10; |
65 |
| - const port = 30000 + Math.floor(Math.random() * 10000); |
66 |
| - const server = http.createServer(requestListener); |
67 |
| - server.listen(port, host, async () => { |
68 |
| - await calculateSum(port, n); |
69 |
| - server.close(); |
70 |
| - }); |
| 67 | + const args = process.argv.slice(2); |
| 68 | + const n = +args[0] || 10; |
| 69 | + const port = 20000 + Math.floor(Math.random() * 30000); |
| 70 | + const server = http.createServer(requestListener); |
| 71 | + server.listen(port, host, async () => { |
| 72 | + await calculateSum(port, n); |
| 73 | + server.close(); |
| 74 | + }); |
71 | 75 | }
|
72 | 76 |
|
73 | 77 | main();
|
0 commit comments