【问题标题】:expressjs 4.x and https, generate key and certificateexpressjs 4.x 和 https,生成密钥和证书
【发布时间】:2014-08-29 20:01:29
【问题描述】:

我想用 express 4.x 创建一个 https 服务器。即使在谷歌上找到的很多代码都是基于 express 3.x 我认为我正确地制作了端口。

即使我尝试使用 goole,我也不太清楚如何生成密钥。没有钥匙,我期待 401。

我尝试使用此gist 中的脚本。但我不断收到错误Error: DEPTH_ZERO_SELF_SIGNED_CERT。

我想用curl、request 和super test 来测试它。

这就是我真正拥有的:

server.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);
});

client.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();

【问题讨论】:

    标签: node.js curl express https supertest


    【解决方案1】:

    使用 cURL,您可以使用 -k 标志绕过自签名证书问题。

    使用request,您只需在请求选项中设置rejectUnauthorized: false。

    【讨论】:

    • ok.. 使用 -k 我得到 {"status":"denied"} 这就是我想要的。但是DEPTH_ZERO_SELF_SIGNED_CERT 呢?那么密钥生成呢?
    • 您说您已经生成了密钥?不过,您可以在服务器端和客户端设置ca。
    • 可能我做错了。你能给我一个脚本吗?
    猜你喜欢
    • 2019-02-05
    • 2019-02-15
    • 2018-12-04
    • 2010-09-15
    • 2011-01-04
    • 2014-01-04
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    相关资源
    最近更新 更多