3

I'm using express framework to run many node applications basically as different node instances. Is it possible to run all the applications as single node instance (like routing with different url and same port) ?

1 Answer 1

5

Sure:

var express = require('express');

var main    = express();
var app1    = express();
var app2    = express();

main.use(app1);
main.use(app2);

app1.get('/app1/test', function(req, res) {
  res.send('handled by app1');
});

app2.get('/app2/test', function(req, res) {
  res.send('handled by app2');
});

main.listen(3012);

If each app has their own unique URL prefix, you can also use this:

var express = require('express');

var main    = express();
var app1    = express();
var app2    = express();

main.use('/app1', app1);
main.use('/app2', app2);

app1.get('/test', function(req, res) { // GET /app1/test
  res.send('handled by app1');
});

app2.get('/test', function(req, res) { // GET /app2/test
  res.send('handled by app2');
});

main.listen(3012);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.