如何使用节点接收http请求。js服务器,然后将请求传递到另一个服务器?

时间:2022-10-30 21:31:49

There's a lot going on here so I'll simplify this into a pseudo example. Forget about security and whatnot for a minute here. The point is to understand the functionality.

这里有很多,我把它化简成一个伪例子。先不谈安全之类的问题。关键是要理解功能。

Let's say I'm running a local web server with node.js to dev a website. In the website, the user should be able to create a new account. The account information will be submitted via ajax to the node server. I then need the node server to take the incoming request and pass it along to another server that gives me access to a database. CouchDB, for example.

假设我正在使用node运行一个本地web服务器。开发一个网站。在网站中,用户应该能够创建一个新帐户。帐户信息将通过ajax提交到节点服务器。然后,我需要节点服务器接收传入的请求,并将其传递给另一个服务器,该服务器允许我访问数据库。例如,CouchDB。

So here's a pseudo example of what I'd like to happen.

这是我想要的伪例子。

In the client's browser:

在客户的浏览器:

$.ajax({
  url: './database_stuff/whatever', // points to the node web server
  method: 'POST',
  data: {name: 'Billy', age: 24}
});

In the Node web server:

在节点web服务器中:

var http     = require('http'),
    dbServer = 'http://127.0.0.1:5984/database_url';

http.createServer(function (req, res) {
  /* figure out that we need to access the database then... */

  // magically pass the request on to the db server
  http.magicPassAlongMethod(req, dbServer, function (dbResponse) {

    // pass the db server's response back to the client
    dbResponse.on('data', function (chunk) {
      res.end(chunk);
    });
  })
}).listen(8888);

Make sense? Basically what's the best way to pass the original request along to another server and then pass the response back to the client?

有意义吗?基本上,将原始请求传递到另一个服务器,然后将响应传递回客户机的最佳方式是什么?

1 个解决方案

#1


3  

If the server at dbServer url supports streaming you could do something like

如果dbServer url的服务器支持流,您可以做类似的事情。

var request = require('request');
req.pipe(request.post(dbServer)).pipe(res)

where request is a module, for more info look here https://github.com/mikeal/request

请求是一个模块,更多信息请参见https://github.com/mikeal/request

This is quite readable and easy to implement, if for whatever reason you cannot do this then you could take what you need from the request and manually POST it, then take the response and res.send it to the client.

这是非常易读和易于实现的,如果由于任何原因您不能这样做,那么您可以从请求中获取您需要的内容并手动发布它,然后获取响应并将其发送给客户端。

Sorry if there's an error in my code, I haven't tested it but my point should be clear, if it's not then ask away.

抱歉,如果我的代码有错误,我还没有测试过,但是我的观点应该是清楚的,如果不是,那就问。

#1


3  

If the server at dbServer url supports streaming you could do something like

如果dbServer url的服务器支持流,您可以做类似的事情。

var request = require('request');
req.pipe(request.post(dbServer)).pipe(res)

where request is a module, for more info look here https://github.com/mikeal/request

请求是一个模块,更多信息请参见https://github.com/mikeal/request

This is quite readable and easy to implement, if for whatever reason you cannot do this then you could take what you need from the request and manually POST it, then take the response and res.send it to the client.

这是非常易读和易于实现的,如果由于任何原因您不能这样做,那么您可以从请求中获取您需要的内容并手动发布它,然后获取响应并将其发送给客户端。

Sorry if there's an error in my code, I haven't tested it but my point should be clear, if it's not then ask away.

抱歉,如果我的代码有错误,我还没有测试过,但是我的观点应该是清楚的,如果不是,那就问。