Node.js 节点http代理和express

Node.js 节点http代理和express,node.js,express,node-http-proxy,Node.js,Express,Node Http Proxy,我正在尝试这样做: // Setup prox to handle blog requests httpProxy.createServer({ hostnameOnly: true, router: { 'http://localhost': '8080', 'http://localhost/blog': '2368' } }).listen(8000); 以前我用过这个: http.createServer(app).listen

我正在尝试这样做:

// Setup prox to handle blog requests
httpProxy.createServer({
    hostnameOnly: true,
    router: {
        'http://localhost': '8080',
        'http://localhost/blog': '2368' 
    }
}).listen(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)

所以我换成了这个,效果更好。问题是express接管了路由(据我所知)

我让它工作了

  • 并确保其工作属性(默认端口为2368)
  • 使用express创建您的节点web应用程序(在端口80上侦听)-此处无特殊说明
  • 在web应用程序中安装
    npm安装http代理
  • 为代理到Ghost服务的请求的/blog*创建通配符路由

    var httpProxy = require('http-proxy');
    
    var proxy = new httpProxy.RoutingProxy();
    app.get('/blog*', function (req, res, next) {
      proxy.proxyRequest(req, res ,{
        host: 'moserlap.splitvr.com',
        port: 2368  
      });
    });
    
  • 更新Ghost配置以使用子目录(仅在0.4.0+中受支持)

  • 你现在应该能够击中和所有路线的工作


将http proxy 1.0与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' });
});

使用
express http proxy
,这是一个非常简单的解决方案,可以无缝工作,还可以与cookie/身份验证一起工作:

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

var blogProxy = proxy('localhost/blog:2368', {
    forwardPath: function (req, res) {
        return require('url').parse(req.url).path;
    }
});
然后简单地说:

app.use("/blog/*", blogProxy);

我知道我参加这个聚会已经晚了,但我希望这对某人有所帮助。

我使用了简单的解决方案来代理我的GET/POST请求

var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();

app.post("/api/*", function(req, res) {
  apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
app.get("/api/*", function(req, res) {
  apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
处理所有类型请求的另一种更简单的方法是:

app.all("/api/*", function(req, res) {
  apiProxy.web(req, res, { target: 'http://localhost:5000'})
});

注意:当这些函数转到
/blog
时,它们必须在bodyparser

之前,我想让它运行承载我博客的Ghost应用程序。。。否则,请运行我的普通站点。在类似问题中,您能否建议我是否有任何解决方案此答案不适用于http proxy 1.0,httpProxy.RoutingProxy不再是一种方法。查看我的更新答案Hello Chandler,这似乎在没有涉及cookie的情况下起作用。你知道我怎样才能让这些课程有效吗?提前谢谢。这怎么能推广到其他http动词,如post\put\delete?@Lucas:据我所知,代理不能与cookie一起使用。Cookie不会发送到同一来源。除非您也重写cookie域/路径。我建议您尝试基于令牌的方法。JWThi,@Chandler。在您的回答中,请求将被代理到*,是否可以代理到目标服务器上的其他路径?e、 g.是否可以添加http_代理作为中间件来表示?当我使用带有端口(在我的例子中为5000)的代理时,应用程序在端口8050上运行,代理仍然在端口8050上代理。我的代码看起来像这个代理('core-api:5000',{/*计算转发路径*/})。您是否测试了上述代码,以及节点所在的端口?@Lo Tan-是的,这是我个人服务器上的工作代码。注意答案中没有显示
localhost/
前缀。我的主要express应用程序正在监听端口80。出于某种原因,我认为代理URL应该显示在chrome开发者工具中。Doh><我遇到了另一个问题(主机后的url路径不正确),它工作正常:)谢谢@罗丹-很乐意帮忙。:)错误:getaddrinfo ENOTFOUND www.google.co.in中的www.google.co.in:80在GetAddrInfoReqWrap.onlookup[as oncomplete](dns.js:77:26)的errnoException(dns.js:26:10)上找到了上述错误。有什么想法吗?
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();

app.post("/api/*", function(req, res) {
  apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
app.get("/api/*", function(req, res) {
  apiProxy.web(req, res, { target: 'http://localhost:5000'})
});
app.all("/api/*", function(req, res) {
  apiProxy.web(req, res, { target: 'http://localhost:5000'})
});