如何通过向另一个Web应用程序发出请求来从express.js提供一些URL?

时间:2022-08-02 16:55:42

I have written some code using express.js which I'd like to put on the same HTTP port as a web application implemented with another technology (in this case, Django). I don't want to have to redirect user browsers to another port since if I do they might bookmark URLs with the other port, and then I lose the ability to reconfigure the arrangements later. So, I'd like express.js to serve HTTP on its port, fulfilling some paths I specify by making HTTP requests to a secondary web application which is being served on another port.

我已经使用express.js编写了一些代码,我想将它放在与使用其他技术(在本例中为Django)实现的Web应用程序相同的HTTP端口上。我不想将用户浏览器重定向到另一个端口,因为如果我这样做,他们可能会用其他端口为URL添加书签,然后我就失去了以后重新配置安排的能力。所以,我希望express.js在其端口上提供HTTP服务,通过向另一个端口上提供的辅助Web应用程序发出HTTP请求来实现我指定的一些路径。

Is there any middleware or other technique for express.js which will serve certain paths by making HTTP requests to other servers?

是否有任何中间件或其他技术可以通过向其他服务器发出HTTP请求来为某些路径提供服务?

(The * question How to make web service calls in Expressjs? is relevant but only discusses GET and I will have some POST requests to forward.)

(*问题如何在Expressjs中进行Web服务调用?是相关的,但只讨论GET,我将有一些POST请求转发。)

2 个解决方案

#1


2  

Whilst it is possible to make POST request with node, I think the pattern you're describing is better suited to using a a server like nginx or apache in front of both node.js and django, and proxying requests to whichever port is appropriate based on the request.

虽然可以使用节点发出POST请求,但我认为您所描述的模式更适合在node.js和django之前使用像nginx或apache这样的服务器,并根据需要将请求代理到任何适当的端口。请求。

Typically, both django and node.js would listen on whichever ports you want them to listen on, while nginx listens on port 80. You then define a virtual host in nginx that forwards certain requests to node.js and certain requests to django.

通常,django和node.js都会侦听您希望它们侦听的端口,而nginx则侦听端口80.然后在nginx中定义一个虚拟主机,将某些请求转发给node.js,并将某些请求转发给django。

Here are the nginx docs on using proxy_pass.

以下是使用proxy_pass的nginx文档。

Here is an example, modified from the nginx Full Example:

这是一个例子,从nginx Full示例修改:

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;

    # serve static files
    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
    }

    # pass requests for dynamic content to django
    location /djangostuff {
      proxy_pass      http://127.0.0.1:8080;
    }

    # pass requests for node
    location /nodestuff {
      proxy_pass      http://127.0.0.1:8081;
    }

  }

#2


1  

With node-http-proxy only a single call to app.use is required to reverse proxy all unhandled requests, like this:

使用node-http-proxy,只需要对app.use进行一次调用即可反向代理所有未处理的请求,如下所示:

var app = express.createServer();
# my app.get bindings
app.use(require('http-proxy').createServer(80, 'other-server-address'));
app.listen(80);

#1


2  

Whilst it is possible to make POST request with node, I think the pattern you're describing is better suited to using a a server like nginx or apache in front of both node.js and django, and proxying requests to whichever port is appropriate based on the request.

虽然可以使用节点发出POST请求,但我认为您所描述的模式更适合在node.js和django之前使用像nginx或apache这样的服务器,并根据需要将请求代理到任何适当的端口。请求。

Typically, both django and node.js would listen on whichever ports you want them to listen on, while nginx listens on port 80. You then define a virtual host in nginx that forwards certain requests to node.js and certain requests to django.

通常,django和node.js都会侦听您希望它们侦听的端口,而nginx则侦听端口80.然后在nginx中定义一个虚拟主机,将某些请求转发给node.js,并将某些请求转发给django。

Here are the nginx docs on using proxy_pass.

以下是使用proxy_pass的nginx文档。

Here is an example, modified from the nginx Full Example:

这是一个例子,从nginx Full示例修改:

  server { # simple reverse-proxy
    listen       80;
    server_name  domain2.com www.domain2.com;

    # serve static files
    location ~ ^/(images|javascript|js|css|flash|media|static)/  {
      root    /var/www/virtual/big.server.com/htdocs;
    }

    # pass requests for dynamic content to django
    location /djangostuff {
      proxy_pass      http://127.0.0.1:8080;
    }

    # pass requests for node
    location /nodestuff {
      proxy_pass      http://127.0.0.1:8081;
    }

  }

#2


1  

With node-http-proxy only a single call to app.use is required to reverse proxy all unhandled requests, like this:

使用node-http-proxy,只需要对app.use进行一次调用即可反向代理所有未处理的请求,如下所示:

var app = express.createServer();
# my app.get bindings
app.use(require('http-proxy').createServer(80, 'other-server-address'));
app.listen(80);