在同一端口上运行多个 Node (Express) 应用程序 [英] Running multiple Node (Express) apps on same port

查看:63
本文介绍了在同一端口上运行多个 Node (Express) 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个 Node 应用程序(构建在 Express 框架上).

I have multiple Node applications (build on Express framework).

现在我是这样放置的 -

Now I have placed them like this -

  • /var/www/app1
  • /var/www/app2
  • /var/www/app3

现在我想在同一个端口(比如 8080)上运行这 3 个应用程序.这可能吗?

Now I want to run these 3 apps on the same port (say 8080). Is that possible ?

需要注意的一点是,每个应用程序都有这样的通用路由 -

One thing to note is that, Each app has common routes like these -

  • app.get('/', func...);
  • app.get('/about', func...);
  • app.post('/foo', func...);
  • app.post('/bar', func...);

基本上,我想像使用 Apache/PHP 设置那样做.

Basically I want to do it like you can do with Apache/PHP setup.

所以当你有一个 LAMP 堆栈时 -

So with a LAMP stack when you have -

  • /var/www/app1
  • /var/www/app2
  • /var/www/app3

您可以轻松地将它们作为不同的应用程序访问 -

You can easily access them as different apps from -

  • localhost/app1
  • localhost/app2
  • localhost/app3

推荐答案

您可以使用 <代码>app.use():

You can use app.use():

app
  .use('/app1', require('./app1/index').app)
  .use('/app2', require('./app2/index').app)
  .listen(8080);

这篇关于在同一端口上运行多个 Node (Express) 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆