我正在尝试启动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);
}
Run Code Online (Sandbox Code Playgroud)
当我去https://localhost:3000,页面抛出一个错误
This site can’t be reached
localhost unexpectedly closed the connection.
ERR_CONNECTION_CLOSED
Run Code Online (Sandbox Code Playgroud)
但是服务器端控制台上没有错误.此外,如果我去常规localhost:3000,我得到:
The localhost page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
提前致谢!
----更新----
我现在正在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.
Run Code Online (Sandbox Code Playgroud)
Val*_*Val -1
你的代码有很多问题。
我很快就测试了这一点。
关键字appandport未定义,分别为第 4 行和第 7 行。
这会引发语法错误,阻止代码继续执行,因此服务器根本无法启动。
正如我在评论中提到的,使用 devtool 进行调试并在 CLI 上使用以下行,在开发时devtool server.js -w监视-w文件更改并动态重新加载服务器。还假设您命名了条目文件server.js