-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttps-client.js
44 lines (36 loc) · 1.05 KB
/
https-client.js
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
import https from 'https'
import { readFileSync } from 'fs'
import path, { resolve } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const options = {
key: readFileSync(resolve(__dirname, '../ca/client-key.pem')),
cert: readFileSync(resolve(__dirname, '../ca/client-crt.pem')),
ca: readFileSync(resolve(__dirname, '../ca/ca-crt.pem')),
}
const host = '127.0.0.1'
const port = 8443
options.host = host
options.port = port
options.method = 'GET'
options.path = '/api'
let request = https.request(options, res => {
if (res.statusCode !== 200) {
console.error(`Did not get an OK from the server. Code: ${res.statusCode}`);
res.resume();
return;
}
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('close', () => {
console.log('Retrieved all data');
console.log(JSON.parse(data));
});
})
request.end();
request.on('error', (err) => {
console.error(`Encountered an error trying to make a request: ${err.message}`);
});