让restify REST API服务器同时支持HTTPS和HTTP

7
我将使用node.js restify ver4.0.3作为翻译,以下简单的代码可作为支持HTTP的REST API服务器。示例API调用为http://127.0.0.1:9898/echo/message。请注意,我会保留HTML标签。
var restify = require('restify');

var server = restify.createServer({
    name: 'myapp',
    version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());

//http://127.0.0.1:9898/echo/sdasd
server.get('/echo/:name', function (req, res, next) {
    res.send(req.params);
    return next();
});

server.listen(9898, function () {
    console.log('%s listening at %s', server.name, server.url);
});

假设我想支持HTTPS并进行API调用https://127.0.0.1:9898/echo/message,应该如何操作?
我注意到restify代码更新很快,旧版本的代码可能无法与最新版本兼容。

1
你有检查过 http://qugstart.com/blog/node-js/node-js-restify-server-with-both-http-and-https/ 吗? - Bas van Stein
谢谢。看起来不错。我正在尝试基于那个链接的示例。目前有一些问题。 - guagay_wk
2个回答

11

使用HTTPS,需要一个密钥和证书:

var https_options = {
  key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
  certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};
var https_server = restify.createServer(https_options);

你需要启动两个服务器,以允许HTTP和HTTPS访问:

http_server.listen(80, function() {
   console.log('%s listening at %s', http_server.name, http_server.url);
});.
https_server.listen(443, function() {
   console.log('%s listening at %s', https_server.name, https_server.url);
});.
配置服务器路由时,需为两个服务器声明相同的路由,并根据需要在 HTTP 和 HTTPS 之间进行重定向。
http_server.get('/1', function (req, res, next) {
    res.redirect('https://www.foo.com/1', next);
});
https_server.get('/1', function (req, res, next) {
    // Process the request   
});

上述代码监听一个路由/1的请求,并将其简单地重定向到处理它的HTTPS服务器。


11

感谢Bas van Stein的评论,以下是一个完整可行的示例。

    var restify = require('restify');
    var fs = require('fs');

    // Setup some https server options
    //generated from http://www.selfsignedcertificate.com/
    var https_options = {
        key: fs.readFileSync('./HTTPS.key'), //on current folder
        certificate: fs.readFileSync('./HTTPS.cert')
    };

    // Instantiate our two servers
    var server = restify.createServer();
    var https_server = restify.createServer(https_options);

    // Put any routing, response, etc. logic here. This allows us to define these functions
    // only once, and it will be re-used on both the HTTP and HTTPs servers
    var setup_server = function(app) {
        function respond(req, res, next) {
            res.send('I see you ' + req.params.name);
        }

        // Routes
        app.get('/test/:name', respond);
    }

    // Now, setup both servers in one step
    setup_server(server);
    setup_server(https_server);

    // Start our servers to listen on the appropriate ports
    server.listen(9848, function() {
        console.log('%s listening at %s', server.name, server.url);
    });

    https_server.listen(443, function() {
        console.log('%s listening at %s', https_server.name, https_server.url);
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接