![](/img/trans.png)
[英]Node.js HTTPS server not responding when using RapidSSL certificate
[英]node.js https server not loading responding
我正在尝试启动https node.js服务器。
我首先按照本指南创建证书和密钥:
http://gaboesquivel.com/blog/2014/nodejs-https-and-ssl-certificate-for-development/
我把它们放在我的/app_name/security/keys
目录中。
要启动我的https服务器,我有以下内容:
const https = require('https'),
fs = require('fs');
if(app.get('env') === 'development') {
console.log('dev env!!'); //prints correctly
console.log('port: ' + port); //prints correctly
const options = {
key: fs.readFileSync('./security/keys/key.pem'),
cert: fs.readFileSync('./security/keys/cert.pem')
};
https.createServer(options, (req, res) => {
console.log('https good to go'); //this does not print out anything
}).listen(port);
}
当我转到https://localhost:3000
,该页面抛出错误
This site can’t be reached
localhost unexpectedly closed the connection.
ERR_CONNECTION_CLOSED
但是服务器端控制台上没有错误。 此外,如果我去常规localhost:3000
,我得到:
The localhost page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
有人可以帮忙吗?
提前致谢!
----更新----
我现在正在443端口上运行。 最初我收到一个错误:
Error: listen EACCES 0.0.0.0:443
所以我跑了:
sudo NODE_ENV=development nodemon app
哪个没有抛出任何错误。 但是,当我去https://localhost:443
,我得到:
This site can’t be reached
localhost unexpectedly closed the connection.
我使用express
作为Web服务器。 安装express
:
npm install express --save
我拿了你的代码,使用openssl
在express
,生成的证书中添加了用法,并执行了 - 一切看起来都不错,服务器启动了,通过https监听端口3000。
我的代码(基于你的代码...):
var app = require('express')();
const https = require('https'),
fs = require('fs'),
port = 3000;
if(app.get('env') === 'development') {
console.log('dev env!!'); //prints correctly
console.log('port: ' + port); //prints correctly
const options = {
key: fs.readFileSync('/tmp/private.key'),
cert: fs.readFileSync('/tmp/publickey.crt')
};
https.createServer(options, (req, res) => {
console.log('https good to go'); //this does message appears!!! ^_^
}).listen(port);
}
请注意我定义app
的方式: var app = require('express')();
如果它更具可读性,您可以将此定义拆分为两行:
var express = require('express');
var app = express();
您的代码存在很多问题。
我很快就测试了这个。
未定义关键字app
和port
,分别为第4行和第7行。
这将导致语法错误,防止代码继续进一步,因此服务器根本不启动。
正如我在评论中提到的那样,使用devtool在CLI devtool server.js -w
上调试并使用以下行,其中-w
devtool server.js -w
文件更改并在开发时动态重新加载服务器。 还假设您将输入文件命名为server.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.