Node.js 使用节点js创建HTTPS服务器

Node.js 使用节点js创建HTTPS服务器,node.js,https,Node.js,Https,我想为本地主机创建https服务器。 NodeJS文档提供了开箱即用的解决方案,但我对它有些困惑。 示例 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服务器。
NodeJS文档提供了开箱即用的解决方案,但我对它有些困惑。 示例

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')
};

这里我如何为我的本地主机获取密钥、cert或pfx?

.pfx
文件是由
key.pem
cert.pem
和有时是(
CA文件
)文件组成的“捆绑包”


您应该获得(支付)/(测试等)https证书。

出于开发目的,您可以创建自认证证书。 下面介绍如何在基于linux的系统上执行此操作:

首先,生成一个私钥

openssl genrsa 1024 > key.pem
这将在key.pem文件中存储1024位RSA密钥

然后,使用该密钥生成SSL证书:

openssl req -x509 -new -key key.pem > key-cert.pem

现在,您可以在传递给createServer的选项中使用key.pem和key-cert.pem。

这称为自签名证书,您可以通过openssl通过一个命令生成它。仅键入:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
在候机楼