在Node JS中,如何创建端点传递?我正在使用快递和http

时间:2015-12-11 04:45:41

标签: javascript node.js endpoints

在Node JS中,如何创建端点传递? 我正在使用express和http 整个应用程序将只是一系列通过端点。 这是我的代码

// the real endpoint is somewhere else. 
// for example http://m-engine.herokuapp.com/api/getstudents2
var http = require('http');
var options = {
   host: 'http://m-engine.herokuapp.com',
   path: '/api/getstudents2',
   method: 'GET'
};



app.get('/api/getstudents', function(req, res){

    // now past the request through
    http.request(options, function(response2) {
        response2.on('data', function (data) {
            res.json(data);
        });
    }).end();

});

1 个答案:

答案 0 :(得分:1)

您可以为node.js使用bouncynode-http-proxy模块来实现相同的目标。

以下是bouncy的示例代码:

var fs = require('fs');
var crypto = require('crypto');
var bouncy = require('bouncy');

bouncy(function (req, bounce) {

        bounce("http://m-engine.herokuapp.com");

}).listen(8000);

虽然,你也可以在nginx的帮助下实现同样的目的。无需为同一件事创建节点服务。在Google上搜索nginx proxy_pass。你可以得到同样的例子。