如何在Node.js中创建HTTPS服务器?

こ雲淡風輕ζ 提交于 2020-03-03 21:05:11

给定一个SSL密钥和证书,如何创建HTTPS服务?


#1楼

Express API文档对此进行了清楚的说明。

此外, 此答案还提供了创建自签名证书的步骤。

我已经从Node.js HTTPS文档中添加了一些注释和摘要

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

// This line is from the Node.js HTTPS documentation.
var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
};

// Create a service (the app object is just a callback).
var app = express();

// Create an HTTP service.
http.createServer(app).listen(80);
// Create an HTTPS service identical to the HTTP service.
https.createServer(options, app).listen(443);

#2楼

谷歌搜索“ node https”时发现了这个问题,但是接受的答案中的示例很旧-取自当前(v0.10)版本的docs ,看起来应该像这样:

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

#3楼

更新资料

通过Greenlock.js使用“让我们加密”

原始帖子

我注意到,这些答案都没有表明在链中添加了中间根CA ,下面是一些零配置示例 ,您可以使用这些示例来查看:

片段:

var options = {
  // this is the private key only
  key: fs.readFileSync(path.join('certs', 'my-server.key.pem'))

// this must be the fullchain (cert + intermediates)
, cert: fs.readFileSync(path.join('certs', 'my-server.crt.pem'))

// this stuff is generally only for peer certificates
//, ca: [ fs.readFileSync(path.join('certs', 'my-root-ca.crt.pem'))]
//, requestCert: false
};

var server = https.createServer(options);
var app = require('./my-express-or-connect-app').create(server);
server.on('request', app);
server.listen(443, function () {
  console.log("Listening on " + server.address().address + ":" + server.address().port);
});

var insecureServer = http.createServer();
server.listen(80, function () {
  console.log("Listening on " + server.address().address + ":" + server.address().port);
});

如果您尝试直接通过connect或express进行操作,但是让本机https模块处理它,然后使用它为您提供连接/ Express应用程序服务,这通常会更容易些。

另外,如果在创建服务器时使用server.on('request', app)而不是通过应用程序,则可以将server实例传递给一些用于创建连接/快速应用程序的初始化函数(如果需要)例如,在同一台服务器上通过ssl进行websocket


#4楼

若要使您的应用程序分别侦听端口80443上的httphttps ,请执行以下操作

创建一个快速应用程序:

var express = require('express');
var app = express();

express()返回的应用程序是一个JavaScript函数。 可以将其作为处理请求的回调传递给Node的HTTP服务器。 这使得使用相同的代码库轻松提供应用程序的HTTP和HTTPS版本。

您可以按照以下方式进行操作:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();

var options = {
  key: fs.readFileSync('/path/to/key.pem'),
  cert: fs.readFileSync('/path/to/cert.pem')
};

http.createServer(app).listen(80);
https.createServer(options, app).listen(443);

有关完整的详细信息,请参阅文档


#5楼

Node.js中HTTPS服务器的最小设置如下所示:

var https = require('https');
var fs = require('fs');

var httpsOptions = {
    key: fs.readFileSync('path/to/server-key.pem'),
    cert: fs.readFileSync('path/to/server-crt.pem')
};

var app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

https.createServer(httpsOptions, app).listen(4433);

如果您还想支持http请求,则只需进行以下小的修改即可:

var http = require('http');
var https = require('https');
var fs = require('fs');

var httpsOptions = {
    key: fs.readFileSync('path/to/server-key.pem'),
    cert: fs.readFileSync('path/to/server-crt.pem')
};

var app = function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

http.createServer(app).listen(8888);
https.createServer(httpsOptions, app).listen(4433);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!