使用 node js 创建 HTTPS 服务器 [英] Create HTTPS server with node js

查看:25
本文介绍了使用 node js 创建 HTTPS 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的本地主机创建一个 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
");
}).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

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

Now, you can use key.pem and key-cert.pem in the options you pass to createServer.

这篇关于使用 node js 创建 HTTPS 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆