在 Node JS 中,如何创建端点直通?我正在使用 express 和 http

In Node JS, how do i create an endpoint pass through? I'm using express and http

在 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();

});

您可以为 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。您可以获得相同的示例。