How to use https

Comprehensive https code examples:

How to use https.Server:

11
12
13
14
15
16
17
18
19
20
21
const options = {
  key: fixtures.readKey('agent1-key.pem'),
  cert: fixtures.readKey('agent1-cert.pem')
};


const server = https.Server(options, common.mustCall((req, res) => {
  res.writeHead(200);
  res.end('Hello, World!');
}));

How to use https.globalAgent:

225
226
227
228
229
230
231
232
233
234
https.globalAgent.options.rejectUnauthorized = false
const socket = io(MC.websocketUrl, {
  withCredentials: true,
  secure: true,
  ...(process.env.SERVER_ENV === 'development') && { ca: Fs.readFileSync(`${MC.repoRoot}/localhost_cert.pem`, 'ascii') },
  agent: https.globalAgent
})
socket.on('disconnect', async () => {
  await AwaitServerReconnect()
  socket.connect()

How to use https.Agent:

1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
  http.close();
  logger.error(`本机${WEB_PORT}端口被其他应用程序占用,请尝试关闭占用${WEB_PORT}端口的其他程序 或 修改配置文件的 WEB_PORT 配置项。错误代码:${err.code}`.error);
  setTimeout(() => StartHttpServer(), 10000);
});


const UnauthorizedHttpsAgent = new https.Agent({ rejectUnauthorized: false }); // #303,Watt Toolkit(Steam++)的自签证书问题


/**
 * 检查本体更新
 */

How to use https.createServer:

53
54
55
56
57
58
59
60
61
62
        const options = { // letsencrypt로 받은 인증서 경로를 입력해 줍니다.
                ca: fs.readFileSync("/etc/letsencrypt/live/massage11gu.com/fullchain.pem"),
                key: fs.readFileSync("/etc/letsencrypt/live/massage11gu.com/privkey.pem"),
                cert: fs.readFileSync("/etc/letsencrypt/live/massage11gu.com/cert.pem")
        };
        https.createServer(options, app).listen(HTTPS_PORT, '0.0.0.0', function () {
                console.log("Server on " + HTTPS_PORT);
        });
}

How to use https.get:

16
17
18
19
20
21
22
23
24
25
 */
const getHttpsSite = (url, timeout) => {
    if(timeout === undefined) timeout = 10000;
    let timeoutId;
    return new Promise((resolve) => {
        const req = https.get(url, function(res) {
            console.log(`Got response: ${ res.statusCode }`, "From", url);
            let content = '';
            res.on('data', function(chunk) {
                content += chunk;

How to use https.request:

29
30
31
32
33
34
35
36
37
38
39
}


async function basicRequest(options, params) {
    return new Promise((resolve, reject) => {
        var data = ""
        const req = https.request(options, (res) => {
            res.on('data', (newData) => {
                data += newData
            })
            res.on('end', () => {