Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

javascript - Running multiple Node (Express) apps on same port

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

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

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use app.use():

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...