I have multiple Node applications (build on Express framework).
我有多个节点应用程序(构建在快速框架上)。
Now I have placed them like this -
现在我把它们放在这里
/var/www/app1
- /var/www/app1
/var/www/app2
- /var/www/app2
/var/www/app3
- /var/www/app3
Now I want to run these 3 apps on the same port (say 8080). Is that possible ?
现在我想在同一个端口上运行这3个应用程序(比如8080)。这有可能吗?
One thing to note is that, Each app has common routes like these -
需要注意的是,每个应用都有像这样的公共路线
app.get('/', func...);
- app.get(' / ',func…);
app.get('/about', func...);
- app.get(' / ',func…);
app.post('/foo', func...);
- app.post(/ foo,func…);
app.post('/bar', func...);
- app.post(“/酒吧”,func…);
Basically I want to do it like you can do with Apache/PHP setup.
基本上,我想做的就像你可以做Apache/PHP设置一样。
So with a LAMP stack when you have -
当你有
/var/www/app1
- /var/www/app1
/var/www/app2
- /var/www/app2
/var/www/app3
- /var/www/app3
You can easily access them as different apps from -
你可以很容易地访问他们作为不同的应用从-
localhost/app1
- localhost / app1
localhost/app2
- localhost / app2
localhost/app3
- localhost / app3
3 个解决方案
#1
49
You can use app.use()
:
您可以使用app.use():
app
.use('/app1', require('./app1/index').app)
.use('/app2', require('./app2/index').app)
.listen(8080);
#2
24
You could run them as seperate apps listening to different ports and then have a proxy (like https://github.com/nodejitsu/node-http-proxy/ ) serving everything on 8080 depending on the requested URL.
您可以将它们作为侦听不同端口的独立应用程序运行,然后有一个代理(比如https://github.com/nodejitsu/node-http-proxy/),根据请求的URL在8080上为所有内容提供服务。
like:
如:
var options = {
router: {
'foo.com/baz': '127.0.0.1:8001',
'foo.com/buz': '127.0.0.1:8002',
'bar.com/buz': '127.0.0.1:8003'
}
};
Works like charm for me ( http://nerdpress.org/2012/04/20/hosting-multiple-express-node-js-apps-on-port-80/). I wasn't so keen on having them mounted as sub-apps, as suggested in the comments because i wanted them to run independently...
《我的魅力》(http://nerdpress.org/2012/04/20/hosting-multiple express-node-js-apps-on-port-80/)。我不太喜欢将它们作为子应用程序安装,正如评论中所建议的那样,因为我想让它们独立运行……
#3
0
You can create one main app(say app) parallel to you apps, and have it initializing the secondary apps (in your case app1, app2, app3) using app.use('', require('./app1/yourApp.js').
您可以创建一个主应用程序(比如app)与您的应用程序并行,并让它使用app.use(“,require('./app1/yourApp.js)初始化辅助应用程序(在您的应用程序中是app1、app2、app3)。
All your apps (app1, app2, app3) need to create app and export it by using var app = module.exports = express(); You need not create instance of server or call app.listen in all the subapps; all the sub-apps can be served via main app listen port.
你所有的应用程序(app1, app2, app3)都需要创建应用程序,然后使用var app =模块导出它。出口=表达();不需要创建服务器实例或调用app.监听所有子应用;所有的子应用都可以通过主应用监听端口进行服务。
#1
49
You can use app.use()
:
您可以使用app.use():
app
.use('/app1', require('./app1/index').app)
.use('/app2', require('./app2/index').app)
.listen(8080);
#2
24
You could run them as seperate apps listening to different ports and then have a proxy (like https://github.com/nodejitsu/node-http-proxy/ ) serving everything on 8080 depending on the requested URL.
您可以将它们作为侦听不同端口的独立应用程序运行,然后有一个代理(比如https://github.com/nodejitsu/node-http-proxy/),根据请求的URL在8080上为所有内容提供服务。
like:
如:
var options = {
router: {
'foo.com/baz': '127.0.0.1:8001',
'foo.com/buz': '127.0.0.1:8002',
'bar.com/buz': '127.0.0.1:8003'
}
};
Works like charm for me ( http://nerdpress.org/2012/04/20/hosting-multiple-express-node-js-apps-on-port-80/). I wasn't so keen on having them mounted as sub-apps, as suggested in the comments because i wanted them to run independently...
《我的魅力》(http://nerdpress.org/2012/04/20/hosting-multiple express-node-js-apps-on-port-80/)。我不太喜欢将它们作为子应用程序安装,正如评论中所建议的那样,因为我想让它们独立运行……
#3
0
You can create one main app(say app) parallel to you apps, and have it initializing the secondary apps (in your case app1, app2, app3) using app.use('', require('./app1/yourApp.js').
您可以创建一个主应用程序(比如app)与您的应用程序并行,并让它使用app.use(“,require('./app1/yourApp.js)初始化辅助应用程序(在您的应用程序中是app1、app2、app3)。
All your apps (app1, app2, app3) need to create app and export it by using var app = module.exports = express(); You need not create instance of server or call app.listen in all the subapps; all the sub-apps can be served via main app listen port.
你所有的应用程序(app1, app2, app3)都需要创建应用程序,然后使用var app =模块导出它。出口=表达();不需要创建服务器实例或调用app.监听所有子应用;所有的子应用都可以通过主应用监听端口进行服务。