node.js 节点 http-proxy 和 express

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20431697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:28:15  来源:igfitidea点击:

Node http-proxy and express

node.jsexpressnode-http-proxy

提问by BRogers

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/blogget taken to the blog but still be served over port 8080(which will eventually be port 80)

基本上,我仍然想使用 express ......但是,当人们去访问http://localhost/blog博客但仍然被服务时port 8080(最终将是端口 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'));
});

回答by Chandler

Using http-proxy 1.0 with express:

将 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' });
});

回答by Selfish

A very straightforward solution which works seamlessly, and with cookies/authentication as well, using express-http-proxy:

一个非常简单的解决方案,可以无缝工作,也可以使用 cookie/身份验证,使用express-http-proxy

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

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

And then simply:

然后简单地:

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

I know I'm late to join this party, but I hope this helps someone.

我知道我加入这个聚会迟到了,但我希望这对某人有所帮助。

回答by Michael Moser

I got this working.

我得到了这个工作。

  • Install Ghostand make sure it's working property (default port is 2368)
  • Create your node web app using express (listen on port 80) - nothing special here
  • Install node-http-proxynpm install http-proxyin your web app
  • Create wildcard route for /blog* that proxies requests to Ghost service

    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  
      });
    });
    
  • Update the Ghost config to use a sub directory (only supported in 0.4.0+)

    config = {
      // ### Development **(default)**
      development: {
      // The url to use when providing links to the site, E.g. in RSS and email.
      url: 'http://127.0.0.1/blog',
    ...
    
  • You should now be able to hit http://yoursite.com/blogand all routes work.

  • 安装 Ghost并确保它的工作属性(默认端口为 2368)
  • 使用 express(侦听端口 80)创建您的节点 Web 应用程序 - 这里没什么特别的
  • 在您的 Web 应用程序中安装node-http-proxynpm install http-proxy
  • 为代理对 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+ 中支持)

    config = {
      // ### Development **(default)**
      development: {
      // The url to use when providing links to the site, E.g. in RSS and email.
      url: 'http://127.0.0.1/blog',
    ...
    
  • 您现在应该可以访问http://yoursite.com/blog并且所有路由都可以正常工作。

回答by Saqy G

I have used simple solution to proxified my GET/POST requests.

我使用了简单的解决方案来代理我的 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'})
});

another easier way to handle all type of requests is:

处理所有类型请求的另一种更简单的方法是:

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

NOTE: above functions must be before bodyparser.

注意:以上函数必须在bodyparser之前。