我想用 express 4.x 创建一个 https 服务器。即使在谷歌上找到的很多代码都是基于 express 3.x 我认为我正确地制作了端口。
即使我尝试使用 goole,我也不太清楚如何生成密钥。没有钥匙,我期待 401。
我尝试使用在这个gist中找到的脚本。但我不断收到错误Error: DEPTH_ZERO_SELF_SIGNED_CERT
。
我想用curl
、request和super test来测试它。
这就是我实际拥有的:
服务器.js
var express = require('express')
, https = require('https')
, fs = require('fs');
var privateKey = fs.readFileSync('./server/server-private-key.pem').toString();
var certificate = fs.readFileSync('./server/server-certificate.pem').toString();
var options = {
key : privateKey
, cert : certificate
}
var app = express();
app.get('/', function(req, res) {
req.client.authorized ?
res.json({"status":"approved"}) :
res.json({"status":"denied"}, 401);
});
server = https.createServer(options,app);
var port = 12345;
server.listen(port, function(){
console.log("Express server listening on port " + port);
});
客户端.js
var https = require('https');
var fs = require('fs');
var options = {
host: 'localhost',
port: 12345,
method: 'GET',
path: '/',
key: fs.readFileSync('./client/client-private-key.pem'),
cert: fs.readFileSync('./client/client-certificate.pem'),
headers: {}
};
var req = https.request(options, function(res) {
console.log('dudee');
console.log(res);
});
req.end();