如何通过向另一个 Web 应用程序发出请求来从 express.js 提供某些 URL?
我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然可以使用节点发出 POST 请求,但我认为您描述的模式更适合在两个节点前面使用像
nginx
或apache
这样的服务器。 js 和 django,并根据请求将请求代理到合适的端口。通常,django 和 node.js 都会侦听您希望它们侦听的任何端口,而 nginx 则侦听端口 80。然后,您在 nginx 中定义一个虚拟主机,将某些请求转发到 node.js,并将某些请求转发到 django。
这里是有关使用 proxy_pass 的 nginx 文档。
下面是一个示例,修改自 nginx 完整示例:
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
orapache
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:
使用 node-http-proxy 只需调用一次 app.use 即可进行反向代理所有未处理的请求,如下所示:
With node-http-proxy only a single call to app.use is required to reverse proxy all unhandled requests, like this: