node.js - 为什么使用 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();
解决方案
您可以像下面这样添加,
this.https = https.Server(options, this.app);
推荐阅读
- swift - 动画 UIView 总是在左上角,即使中心在其他地方
- raspberry-pi - 覆盆子如何在不知道纬度/经度的情况下找到其本地时区
- php - 在主题中加载特定的 WooCommerce 模板
- bash - 将代码拆分为文件
- unity3d - 为什么我的搅拌机 .fbx 文件没有导出到 Unity?
- asp.net-core - 如何在 Asp net core 3.1 中通过身份服务器 4 中的用户 ID 获取 access_token、刷新令牌
- java - 导入比主 gradle 项目文件高一级的额外项目
- c# - 是否可以在 C# 中为 KeyValuePairs 的 LinkedList 创建序列化代理?
- r - R循环使用data.table标准化变量
- postgresql - 如何更改露天开始目录?