Jac*_*ble 469
该快递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);
Run Code Online (Sandbox Code Playgroud)
hvg*_*des 147
我发现了以下例子.
这适用于节点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);
Run Code Online (Sandbox Code Playgroud)
pky*_*eck 85
在谷歌搜索"节点https"时发现这个问题,但接受的答案中的例子非常陈旧 - 取自当前(v0.10)版本的节点的文档,它应该如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
小智 48
上面的答案是好的,但使用Express和节点,这将很好.
由于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});
Run Code Online (Sandbox Code Playgroud)
Joh*_*ers 19
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);
Run Code Online (Sandbox Code Playgroud)
如果您还想支持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);
Run Code Online (Sandbox Code Playgroud)
Coo*_*J86 18
通过Greenlock.js使用Let的加密
我注意到这些答案都没有显示向链中添加中间根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);
});
Run Code Online (Sandbox Code Playgroud)
如果您不尝试通过connect或express直接执行此操作,这通常会更容易,但让本机https
模块处理它然后使用它来为您提供连接/表达应用程序.
此外,如果您server.on('request', app)
在创建服务器时使用而不是传递应用程序,它会让您有机会将server
实例传递给创建connect/express应用程序的某个初始化函数(如果您想在同一服务器上通过ssl 执行websockets,例).
为了使您的应用程序监听双方http
和https
有关港口80
和443
分别执行以下操作
创建快速应用:
var express = require('express');
var app = express();
Run Code Online (Sandbox Code Playgroud)
返回的应用程序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);
Run Code Online (Sandbox Code Playgroud)
有关完整详细信息,请参阅文档
归档时间: |
|
查看次数: |
362578 次 |
最近记录: |