简体   繁体   English

在Node.js Express中运行https服务器时出现ERR_SSL_PROTOCOL_ERROR浏览器错误消息

[英]ERR_SSL_PROTOCOL_ERROR browser error message while running a https server in nodejs express

I have a number of nodejs applications running on Express using the code below. 我使用下面的代码在Express上运行了许多nodejs应用程序。 They all run fine using code similar to the following: 使用类似于以下内容的代码,它们都可以正常运行:

fs = require 'fs'                                                               
https = require 'https'                                                         
express = require 'express'                                                     
server_port = 3000                                                              
keys_dir = 'keys/'                                                              server_options = {
  key  : fs.readFileSync(keys_dir + 'privatekey.pem'), 
  cert : fs.readFileSync(keys_dir + 'certificate.pem')                          }                                                                                             app = express.createServer(server_options)
app.listen server_port 
console.log "HTTPS Server started on port #{server_port}"  

However, when trying to create a new application using this code I see a ERR_SSL_PROTOCOL_ERROR when starting the https server. 但是,当尝试使用此代码创建新应用程序时,在启动https服务器时看到ERR_SSL_PROTOCOL_ERROR。 Any idea what is causing this problem? 知道是什么引起了这个问题吗?

I discovered that was caused when moving from express 2.5.8 to express 3 - specifically 3.0.0beta4. 我发现这是由于从Express 2.5.8迁移到Express 3-特别是3.0.0beta4而引起的。 When creating a new project the version pulled from npm had changed to the version 3 series. 创建新项目时,从npm提取的版本已更改为版本3系列。 Even though the module is marked as "beta" when you run express --version this version is what is installed now when running npm install express . 即使在运行express --version时模块被标记为“ beta”,该版本仍是运行npm install express时现在安装的版本。 The details of the changes are outlined here . 此处概述了更改的详细信息。

To solve this for my case I used the following code: 为了解决这个问题,我使用了以下代码:

const fs = require("fs");
const https = require("https");
const express = require("express");

const keysDir = "keys/";
const options = {
  key  : fs.readFileSync(keysDir + "privatekey.pem"),
  ca   : fs.readFileSync(keysDir + "certrequest.csr"),
  cert : fs.readFileSync(keysDir + "certificate.pem")
};

const app = express();
https.createServer(options, app).listen(3000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM