首页 > 解决方案 > 为什么使用 HTTPS 运行 NPM 时出现错误?

问题描述

我已经定义了 SSL 证书的选项,但我不知道如何在代码中使用它。所以我没有通过 HTTPS URL 获得正确的结果。 这是我的代码:

'use strict';

const express = require('express');
const https = require('https'), fs = require("fs");
const socketio = require('socket.io');
const socketEvents = require('./utils/socket');


const options = {
  key: fs.readFileSync("/www/wwwroot/securechat.online/nodejs/ssl/privatekey.pem"),
  cert: fs.readFileSync("/www/wwwroot/securechat.online/nodejs/ssl/cert1.pem")
};

class Server {
    constructor() {
        this.port = process.env.PORT || 8099;
        this.host = process.env.HOST || 'securechat.online';

        this.app = express();
        this.https = https.Server(this.app);
        this.socket = socketio(this.https);
    }

    appRun(){
        new socketEvents(this.socket).socketConfig();
        this.app.use(express.static(__dirname + '/uploads'));
        this.https.listen(this.port, this.host, () => {
            console.log(`Listening on https://${this.host}:${this.port}`);
        });
    }
}

const app = new Server();
app.appRun();

标签: node.jshttpssocket.io

解决方案


您可以像下面这样添加,

this.https = https.Server(options, this.app);

推荐阅读