Nodejs - 在本地将多个应用程序代理到单个 url [英] Nodejs - Proxy multiple applications to a single url locally
问题描述
我想让多个 nodejs 应用程序都侦听代理到本地机器上的 URL 的不同端口.
I'd like to have multiple nodejs applications all listening on different ports proxied to a URL on my local machine.
一个例子是
localhost:3000 -> mysite.dev本地主机:3030 -> mysite.dev/api
localhost:3000 -> mysite.dev localhost:3030 -> mysite.dev/api
这将使本地开发与我的生产设置相匹配,并对我的无状态身份验证 setep 有很大帮助.我认为 hotel 将成为我想要的解决方案,但它并没有完全做到什么我在找.
It would make developing locally match my production setup and help immensely with my stateless authentication setep. I thought hotel was going to be the solution I wanted, but it doesn't do exactly what I'm looking for.
如果可能,我想避免在本地使用 nginx.
If possible I'd like to avoid using nginx locally.
推荐答案
你可以试试 http-proxy
模块,你可以指定代理为
You can try http-proxy
module and you can specify proxy as
var http = require('http'),
httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); // See (†)
//
// Create your target server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
他们在这里有很好的文档 https://github.com/nodejitsu/node-http-代理
they have nice docs available here https://github.com/nodejitsu/node-http-proxy
这篇关于Nodejs - 在本地将多个应用程序代理到单个 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!