在节点JS中,我如何创build一个端点传递? 我使用快递和http

在节点JS中,我如何创build一个端点传递? 我使用快递和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的bouncy或者node-http-proxy模块来实现同样的function。

以下是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的帮助下达到同样的效果。 不需要为同一件事情创build节点服务。 在Google上searchnginx proxy_pass 。 你可以得到相同的例子。