使用节点js创建HTTPS服务器 [英] Create HTTPS server with node js
问题描述
我想为我的localhost创建一个https服务器。
Node JS文档提供了现成的解决方案,但我对它有些困惑。
示例
I want to create a https server for my localhost.
Node JS documentation provides out of the box solution but I have some confusion with it.
Example
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
或
var options = {
pfx: fs.readFileSync('server.pfx')
};
这里我如何获得本地主机的密钥,证书或pfx?
Here how would I get key, cert or pfx for my localhost?
推荐答案
出于开发目的,您可以创建自我认证的证书。
以下是如何在基于Linux的系统上执行此操作:
For development purposes you can create a self-certified certificate. Here's how to do it on a linux-based system:
首先,生成私钥
openssl genrsa 1024 > key.pem
这将在文件key.pem中存储1024位RSA密钥
This will store a 1024 bit RSA key in the file key.pem
然后,使用该密钥生成SSL证书:
Then, generate an SSL certificate with that key:
openssl req -x509 -new -key key.pem > key-cert.pem
现在,您可以使用key.pem和key-cert.pem您传递给createServer的选项。
Now, you can use key.pem and key-cert.pem in the options you pass to createServer.
这篇关于使用节点js创建HTTPS服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!