【问题标题】:Error on launching https server express启动 https server express 时出错
【发布时间】:2015-12-23 04:01:40
【问题描述】:

我在 Mac OS X 上运行 node v4.1.1 和 npm 2.14.4。对于同一个 express 应用程序,我想启动一个 http 和 https 服务器。 http 运行得非常完美,但另一个崩溃并导致错误:

if (process.features.tls_npn && !opts.NPNProtocols) {
                                       ^
TypeError: Cannot read property 'NPNProtocols' of undefined
at new Server (https.js:13:40)
at Object.exports.createServer (https.js:37:10)

以下代码运行整个 express 应用并调用 http 方法:

#!/usr/bin/env node

var app = require('./src')
, config = require('./config')
, cmd = require('commander')
, http = require('http')
, https = require('https')
, path = require('path')

cmd
.version('0.1.42')
.option('-p, --port <n>', 'Port to start the HTTP server', parseInt)
.parse(process.argv)

// Launch server with web sockets
var server = http.createServer(app)
var sslServer = https.createServer({
    key: fs.readFileSync('ryans-ley.pem'),
    cert: fs.readFileSync('ryans-cert.pem')
}, app)

// Listen on provided port, on all network interfaces.
server.listen(config.port || cmd.port, function () {
  console.log('http marketplace started on %s:%s', 
    server.address().address,
    server.address().port)
})

sslServer.listen(443, function () {
  console.log('https marketplace started on %s:%s', 
    sslServer.address().address,
    sslServer.address().port)
})

这些证书是通过本指南生成的:https://nodejs.org/api/tls.html#tls_tls_ssl

如果我运行相同的代码,但不向 https.createServer 提供选项,它不会崩溃。但是,当尝试访问 https://localhost 时,浏览器会以 ERR_SSL_VERSION_OR_CIPHER_MISMATCH(chrome 浏览器) 回答

带卷曲

curl https://localhost
curl: (35) Unknown SSL protocol error in connection to localhost:-9824

【问题讨论】:

  • 同样的问题,除了我有一个“官方”证书/密钥对。你有什么进展吗?
  • 是的,它碰巧是一个错字。不过真的很安静。如果 key + cert 配对良好并且完全可读,则应该没有错误。仔细检查路径。

标签: ssl express https tls1.2


【解决方案1】:

证书或链无效,但错误未反映它。 因此,对于遇到相同问题的任何人,请尝试使用其他密钥对。

【讨论】:

【解决方案2】:

在选项中设置允许的协议为我解决了这个问题:

NPNProtocols: ['http/2.0', 'spdy', 'http/1.1', 'http/1.0']

因此,您的代码示例中的服务器实例化如下所示:

var sslServer = https.createServer({
    key: fs.readFileSync('ryans-ley.pem'),
    cert: fs.readFileSync('ryans-cert.pem'),
    NPNProtocols: ['http/2.0', 'spdy', 'http/1.1', 'http/1.0']
}, app)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多