假设您在 Heroku 上并且仅限于 Node.js 应用程序,我建议您立即启动一个新节点作为反向代理。一个快速而肮脏的例子如下:
代理.js
var http = require('http'),
httpProxy = require('http-proxy');
var options = {
pathnameOnly: true,
router: {
'/foo': '127.0.0.1:8001',
'/bar': '127.0.0.1:8002'
}
};
var proxyServer = httpProxy.createServer(options);
proxyServer.listen(8000);
第一个.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('I am the first server!\n');
}).listen(8001);
第二个.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('I am the second server!\n');
}).listen(8002);
三个脚本都用node启动,测试结果如下:
cloud@wishlist:~$ curl localhost:8000/foo
I am the first server!
cloud@wishlist:~$ curl localhost:8000/bar
I am the second server!
这正是您所需要的:让两个应用程序看起来像是在同一个端口上监听的东西。有关更多详细信息,请查看node http-proxy 模块