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

查看:289
本文介绍了在同一个端口上运行多个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

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

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

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 ...);

  • 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

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

您可以轻松地从 -

You can easily access them as different apps from -


  • localhost / app1

  • localhost / app2

  • localhost / app3

  • 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天全站免登陆