使用快速服务器

时间:2015-10-09 10:50:21

标签: node.js express

我正在遵循在节点内设置ssl的本教程。 http://blog.nategood.com/nodejs-ssl-client-cert-auth-api-rest

此处通过将选项对象传递给createServer

来加载证书文件
var options = {
    key:    fs.readFileSync('ssl/server.key'),
    cert:   fs.readFileSync('ssl/server.crt'),
    ca:     fs.readFileSync('ssl/ca.crt'),
    requestCert:        true,
    rejectUnauthorized: false
};

https.createServer(options, function (req, res) {
    if (req.client.authorized) {
        res.writeHead(200, {"Content-Type": "application/json"});
        res.end('{"status":"approved"}');
    } else {
        res.writeHead(401, {"Content-Type": "application/json"});
        res.end('{"status":"denied"}');
    }
}).listen(443);

但是我正在使用express来满足我的应用服务器需求。

流程如下:

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

app.get('/', function(req, res){
  res.send('hello world');
});

app.listen(3000);

我在哪里传递证书详情?

1 个答案:

答案 0 :(得分:0)

只需在https.createServer回调中传递快速应用程序对象:

var express = require('express');
var app = express();
var options = {
    key:    fs.readFileSync('ssl/server.key'),
    cert:   fs.readFileSync('ssl/server.crt'),
    ca:     fs.readFileSync('ssl/ca.crt'),
    requestCert:        true,
    rejectUnauthorized: false
};

app.get('/', function(req, res){
  res.send('hello world');
});

https.createServer(options, app).listen(3000);