【问题标题】:Why am I getting an error to run NPM with HTTPS?为什么使用 HTTPS 运行 NPM 时出现错误?
【发布时间】:2021-01-18 12:49:31
【问题描述】:

我已经定义了 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.js https socket.io


    【解决方案1】:

    你可以像下面这样添加,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多