鉴于SSL密钥和证书,如何创建HTTPS服务?
答案 0 :(得分:443)
Express API doc非常清楚地解释了这一点。
此外,this answer提供了创建自签名证书的步骤。
我在Node.js HTTPS documentation添加了一些评论和摘要:
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);
答案 1 :(得分:136)
我找到了以下示例。
这适用于节点v0.1.94 - v0.3.1。在较新版本的节点中删除了server.setSecure()
。
直接来自该来源:
const crypto = require('crypto'),
fs = require("fs"),
http = require("http");
var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();
var credentials = crypto.createCredentials({key: privateKey, cert: certificate});
var handler = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
};
var server = http.createServer();
server.setSecure(credentials);
server.addListener("request", handler);
server.listen(8000);
答案 2 :(得分:83)
在谷歌搜索“节点https”时发现此问题,但accepted answer中的示例非常陈旧 - 取自当前(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 :(得分:48)
上述答案很好,但使用Express和节点,这样可以正常工作。
快递为你创建应用程序,我会在这里跳过它。
var express = require('express')
, fs = require('fs')
, routes = require('./routes');
var privateKey = fs.readFileSync('cert/key.pem').toString();
var certificate = fs.readFileSync('cert/certificate.pem').toString();
// To enable HTTPS
var app = module.exports = express.createServer({key: privateKey, cert: certificate});
答案 4 :(得分:18)
我注意到这些答案都没有显示向链中添加中间根CA ,这里有一些零配置示例可以看到:< / p>
段:
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
模块处理它,然后使用它来连接/表达应用程序。
此外,如果您在创建服务器时使用server.on('request', app)
而不是传递应用程序,它会让您有机会将server
实例传递给创建connect / express应用程序的某个初始化程序函数(如果你想在同一台服务器上通过ssl做 websockets 。)。
答案 5 :(得分:17)
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);
答案 6 :(得分:7)
要让您的应用分别在端口http
和https
上同时收听80
和443
,请执行以下操作
创建快速应用:
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);
有关完整详细信息,请参阅doc
答案 7 :(得分:0)
您还可以使用Fastify框架对此进行存档:
adult.replace('?', float('nan'))
(如果需要编写测试,请运行const { readFileSync } = require('fs')
const Fastify = require('fastify')
const fastify = Fastify({
https: {
key: readFileSync('./test/asset/server.key'),
cert: readFileSync('./test/asset/server.cert')
},
logger: { level: 'debug' }
})
fastify.listen(8080)
创建文件)
答案 8 :(得分:0)
如果您仅在本地需要它进行本地开发,那么我已经为该任务创建了实用工具-https://github.com/pie6k/easy-https
import { createHttpsDevServer } from 'easy-https';
async function start() {
const server = await createHttpsDevServer(
async (req, res) => {
res.statusCode = 200;
res.write('ok');
res.end();
},
{
domain: 'my-app.dev',
port: 3000,
subdomains: ['test'], // will add support for test.my-app.dev
openBrowser: true,
},
);
}
start();
它:
答案 9 :(得分:-3)
答案 10 :(得分:-4)
var path = require('path');
var express = require('express');
var app = express();
var staticPath = path.join(__dirname, '/public');
app.use(express.static(staticPath));
app.listen(8070, function() {
console.log('Server started at port 8070');
});