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

发布于 2024-11-30 13:54:12 字数 476 浏览 2 评论 0原文

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

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

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

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2024-12-07 13:54:12

虽然可以使用节点发出 POST 请求,但我认为您描述的模式更适合在两个节点前面使用像 nginxapache 这样的服务器。 js 和 django,并根据请求将请求代理到合适的端口。

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

这里是有关使用 proxy_pass 的 nginx 文档。

下面是一个示例,修改自 nginx 完整示例

  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;
    }

  }

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.

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.

Here are the nginx docs on using proxy_pass.

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

  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;
    }

  }
厌倦 2024-12-07 13:54:12

使用 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);

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

var app = express.createServer();
# my app.get bindings
app.use(require('http-proxy').createServer(80, 'other-server-address'));
app.listen(80);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文