代理与express.js [英] Proxy with express.js

查看:158
本文介绍了代理与express.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了避免同一个域AJAX问题,我希望我的node.js Web服务器将所有请求从URL / api / BLABLA 转发到另一个服务器,例如 other_domain.com:3000/BLABLA ,并向用户返回与该远程服务器返回的相同的内容。



所有其他网址( / api / * 旁边)将直接投放,无代理。



如何使用node.js + express.js实现这一点?你可以给出一个简单的代码示例吗?



(我的控制下,Web服务器和远程 3000 服务器,两个运行node.js与express.js)






到目前为止,我发现这个 https://github.com/nodejitsu/node-http-proxy/ ,但阅读文档没有我更聪明我结束了

  var proxy = new httpProxy.RoutingProxy(); 
app.all(/ api / *,function(req,res){
console.log(old request url+ req.url)
req.url ='/ '+ req.url.split('/')。slice(2).join('/'); //删除'/ api'部分
console.log(new request url+ req。 url)
proxy.proxyRequest(req,res,{
host:other_domain.com,
port:3000
});
});

但没有任何内容返回给原始的Web服务器(或最终用户),所以没有运气。

解决方案

你想使用 http.request 创建类似的请求到远程API并返回其响应。



这样的东西:

  var http = require('http'); 

/ *您的应用程序配置在这里* /

app.post('/ api / BLABLA',function(req,res){

var options = {
// host to forward to
host:'www.google.com',
// port to forward to
port:80,
// path to forward to
path:'/ api / BLABLA',
// request method
method:'POST',
// header to send
标题:req.headers
};

var creq = http.request(options,function(cres){

// set encoding
cres .setEncoding('utf8');

//等待数据
cres.on('data',function(chunk){
res.write(chunk);
});

cres.on('close',function(){
//关闭,让我们结束客户端请求以及
res.writeHead(cres。 statusCode);
res.end();
});

cres.on('end',function(){
//完成,客户请求以及
res .writeHead(cres.statusCode);
res.end();
});

})。on('error',function(e){
//我们收到错误,返回500错误给客户端和日志错误
console.log .message);
res.writeHead(500);
res.end();
});

creq.end();

});

注意:我没有真正尝试过上述内容,所以可能会包含解析错误这将给你一个如何让它上班的提示。


To avoid same-domain AJAX issues, I want my node.js web server to forward all requests from URL /api/BLABLA to another server, for example other_domain.com:3000/BLABLA, and return to user the same thing that this remote server returned, transparently.

All other URLs (beside /api/*) are to be served directly, no proxying.

How do I achieve this with node.js + express.js? Can you give a simple code example?

(both the web server and the remote 3000 server are under my control, both running node.js with express.js)


So far I found this https://github.com/nodejitsu/node-http-proxy/ , but reading the documentation there didn't make me any wiser. I ended up with

var proxy = new httpProxy.RoutingProxy();
app.all("/api/*", function(req, res) {
    console.log("old request url " + req.url)
    req.url = '/' + req.url.split('/').slice(2).join('/'); // remove the '/api' part
    console.log("new request url " + req.url)
    proxy.proxyRequest(req, res, {
        host: "other_domain.com",
        port: 3000
    });
});

but nothing is returned to the original web server (or to the end user), so no luck.

解决方案

You want to use http.request to create a similar request to the remote API and return its response.

Something like this:

var http = require('http');

/* your app config here */

app.post('/api/BLABLA', function(req, res) {

  var options = {
    // host to forward to
    host:   'www.google.com',
    // port to forward to
    port:   80,
    // path to forward to
    path:   '/api/BLABLA',
    // request method
    method: 'POST',
    // headers to send
    headers: req.headers
  };

  var creq = http.request(options, function(cres) {

    // set encoding
    cres.setEncoding('utf8');

    // wait for data
    cres.on('data', function(chunk){
      res.write(chunk);
    });

    cres.on('close', function(){
      // closed, let's end client request as well 
      res.writeHead(cres.statusCode);
      res.end();
    });

    cres.on('end', function(){
      // finished, let's finish client request as well 
      res.writeHead(cres.statusCode);
      res.end();
    });

  }).on('error', function(e) {
    // we got an error, return 500 error to client and log error
    console.log(e.message);
    res.writeHead(500);
    res.end();
  });

  creq.end();

});

Notice: I haven't really tried the above, so it might contain parse errors hopefully this will give you a hint as to how to get it to work.

这篇关于代理与express.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆