forked from HowProgrammingWorks/IntegrationTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
36 lines (31 loc) · 854 Bytes
/
client.js
File metadata and controls
36 lines (31 loc) · 854 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 api = {};
const methods = ['startCounter', 'stopCounter'];
const createMethod = (name) => (...args) => new Promise((resolve, reject) => {
const req = http.request({
hostname: 'localhost',
port: 8000,
path: `/api/${name}`,
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}, async (res) => {
if (res.statusCode !== 200) {
reject();
return;
}
res.setEncoding('utf8');
const buffers = [];
res.on('data', (chunk) => {
buffers.push(chunk);
}).on('end', () => {
const data = Buffer.concat(buffers).toString();
resolve(JSON.parse(data));
}).on('error', reject);
});
req.end(JSON.stringify(args));
});
for (const method of methods) {
api[method] = createMethod(method);
}
module.exports = { api };