2015-12-11 32 views
2

在節點JS中,我如何創建一個端點通過? 我使用快遞和http 整個應用程序將只是一系列通過端點。 這裏是我的代碼在Node JS中,我如何創建端點傳遞?我使用快遞和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(); 

}); 
+0

因此,無論何時任何api請求通過api endpoint/api/*到達域x,您都希望將它重定向到帶有endpoint/api/*的http://m-engine.herokuapp.com,這是您要實現的目標? –

+0

難道你只是做'response2.pipe(res)',因爲你只是想讓它通過'可讀'和'可寫'流? – shriek

+0

@HirenS。沒有沒有重定向,通過 – PrimeLens

回答

1

您可以使用bouncynode-http-proxy模塊node.js的實現同樣的事情。

這裏是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的幫助也。無需爲同一事物創建節點服務。谷歌搜索nginx proxy_pass。你可以得到相同的例子。

+0

感謝您的回覆。必須是節點,對不起。你的代碼沒有意義。你的意思是app.get中的彈性嵌套嗎? – PrimeLens

+0

也一直在使用其他庫,如express-http-proxy – PrimeLens