节点http-proxy和express [英] Node http-proxy and express

查看:167
本文介绍了节点http-proxy和express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这样做:

  //设置prox来处理博客请求
httpProxy .createServer({
hostnameOnly:true,
router:{
'http:// localhost':'8080',
'http:// localhost / blog' 2368'
}
})。听(8000);

以前我在使用这个:

  http.createServer(app).listen(app.get('port'),function(){
console.log(Express server listening on port+ app.get ('port'));
});

基本上,我还想使用express ...但是当人们去 http:// localhost / blog 获取到博客,但仍然通过端口8080 (最终将是端口80) / p>

所以我把它切换到这个,它工作得更好。问题是,快递接管路由(从我可以看出)

  var options = {
// pathnameOnly:true,
路由器:{
'localhost':'localhost:8080',
'localhost / blog':'localhost:2368'
}
}

//设置prox来处理博客请求
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(9000);

require('./ app / server / router')(app);

http.createServer(app).listen(app.get('port'),function(){
console.log(Express server listening on port+ app.get 'port'));
});


解决方案

使用http-proxy 1.0 with express:

  var httpProxy = require('http-proxy'); 

var apiProxy = httpProxy.createProxyServer();

app.get(/ api / *,function(req,res){
apiProxy.web(req,res,{target:'http://google.com: 80'});
});


I'm trying to do something like this:

// Setup prox to handle blog requests
httpProxy.createServer({
    hostnameOnly: true,
    router: {
        'http://localhost': '8080',
        'http://localhost/blog': '2368' 
    }
}).listen(8000);

Previously I was using this:

http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});

Basically, I want to still use express... but, when people go to http://localhost/blog get taken to the blog but still be served over port 8080 (which will eventually be port 80)

So I switched it to this and it worked better. The problem is that express takes over the routing (from what I can tell)

var options = {
    // pathnameOnly: true,
    router: {
        'localhost': 'localhost:8080',
        'localhost/blog': 'localhost:2368'
    }
}

// Setup prox to handle blog requests
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(9000);

require('./app/server/router')(app);

http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port'));
});

解决方案

Using http-proxy 1.0 with express:

var httpProxy = require('http-proxy');

var apiProxy = httpProxy.createProxyServer();

app.get("/api/*", function(req, res){ 
  apiProxy.web(req, res, { target: 'http://google.com:80' });
});

这篇关于节点http-proxy和express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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