node.js 如何为 express.js 服务器设置 SSL 证书?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11804202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I setup a SSL certificate for an express.js server?
提问by murvinlai
Before, in an older version of express, I could do this:
以前,在较旧版本的 express 中,我可以这样做:
express.createServer({key:'keyFile', cert:'certFile'});
However, in newer versions of express this no longer works:
但是,在较新版本的 express 中,这不再有效:
var app = express();
Should I call app.use()to set the certs? If so how?
我应该打电话app.use()来设置证书吗?如果是这样怎么办?
回答by ebohlman
See the Express docsas well as the Node docs for https.createServer(which is what express recommends to use):
请参阅Express 文档以及https.createServer的Node 文档(这是 express 推荐使用的):
var privateKey = fs.readFileSync( 'privatekey.pem' );
var certificate = fs.readFileSync( 'certificate.pem' );
https.createServer({
key: privateKey,
cert: certificate
}, app).listen(port);
Other options for createServer are at: http://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
createServer 的其他选项位于:http: //nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
回答by geoffreak
I was able to get SSL working with the following boilerplate code:
我能够让 SSL 使用以下样板代码:
var fs = require('fs'),
http = require('http'),
https = require('https'),
express = require('express');
var port = 8000;
var options = {
key: fs.readFileSync('./ssl/privatekey.pem'),
cert: fs.readFileSync('./ssl/certificate.pem'),
};
var app = express();
var server = https.createServer(options, app).listen(port, function(){
console.log("Express server listening on port " + port);
});
app.get('/', function (req, res) {
res.writeHead(200);
res.end("hello world\n");
});
回答by hoogw
This is my working codefor express 4.0.
这是我的express 4.0工作代码。
express 4.0 is very different from 3.0 and others.
express 4.0 与 3.0 和其他版本有很大不同。
4.0 you have /bin/www file, which you are going to add https here.
4.0 你有 /bin/www 文件,你将在这里添加 https。
"npm start" is standard way you start express 4.0 server.
“npm start”是启动 express 4.0 服务器的标准方式。
readFileSync() function should use __dirnameget current directory
readFileSync() 函数应该使用__dirname获取当前目录
while require() use ./refer to current directory.
而 require()使用 ./指的是当前目录。
First you put private.key and public.cert file under /bin folder, It is same folder as WWW file.
首先你把private.key 和public.cert 文件放在/bin 文件夹下,它和WWW 文件是同一个文件夹。
no such directory found error:
没有找到这样的目录错误:
key: fs.readFileSync('../private.key'),
cert: fs.readFileSync('../public.cert')
error, no such directory found
错误,没有找到这样的目录
key: fs.readFileSync('./private.key'),
cert: fs.readFileSync('./public.cert')
Working code should be
工作代码应该是
key: fs.readFileSync(__dirname + '/private.key', 'utf8'),
cert: fs.readFileSync(__dirname + '/public.cert', 'utf8')
Complete https code is:
完整的https代码是:
const https = require('https');
const fs = require('fs');
// readFileSync function must use __dirname get current directory
// require use ./ refer to current directory.
const options = {
key: fs.readFileSync(__dirname + '/private.key', 'utf8'),
cert: fs.readFileSync(__dirname + '/public.cert', 'utf8')
};
// Create HTTPs server.
var server = https.createServer(options, app);

