-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (34 loc) · 915 Bytes
/
index.js
File metadata and controls
43 lines (34 loc) · 915 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
37
38
39
40
41
42
import http from 'node:http';
async function req({
url = 'localhost',
port,
path = '/',
method = 'GET',
headers = {},
})
{
const options = {
hostname: url,
port: port,
path: path,
method,
headers,
};
return new Promise(( resolve, reject ) => {
console.log( `Making ${method} request to ${url}:${port}` );
const req = http.request( options, res => {
res.setEncoding( 'utf8' );
let data = '';
res.on( 'data', chunk => data += chunk );
res.on( 'end', () => {
console.log( `[${method}] ${url}:${port}${path} Response: ${data}` );
resolve( data );
});
});
req.on( 'error', err => {
reject( `Error in request [${method}] ${url}:${port}${path}` );
});
req.end();
});
}
export { req };