Saransh Mohapatra
Saransh Mohapatra

Reputation: 9636

Two apps in expressjs

I am building an app with express js which will have different clients like web and mobile. I didnt want to use one app for both as some middleware would be additional burden. For say like session middleware. So is it possible for one project to have two apps. And how would it work?

Upvotes: 3

Views: 3063

Answers (3)

Plato
Plato

Reputation: 11052

I wanted to share a different approach that I used in a project recently:

function renderAppropriate(template1, template2){
  return function(req, res){
    if(req.session && req.session.mobileOn){
      res.render(template1);
    } else {
      res.render(template2);
    }
  };
};

app.get('/', function(req, res, next){
  // do some stuff
  next()
}, renderAppropriate('someMobileTemplate', 'someDesktopTemplate')
);

Upvotes: -1

Plato
Plato

Reputation: 11052

The app object that you make in express is a function(req,res,next) that is suitable for Express's own middleware chains. So you can use app.use to send requests matching a leading path fragment to an app defined elsewhere.

Docs: http://expressjs.com/api.html#app.use

$ npm install express

//mobile.js
var app = require('express')();
app.get('/', function(req, res){ 
  res.send('Mobile Route') 
});
module.exports = app;


//desktopApp.js
var http = require('http');
var express = require('express');
var desktopApp = express();
var mobileApp = require('./mobile.js');

desktopApp.use('/mobile', mobileApp)
desktopApp.use(desktopApp.router);
desktopApp.use(express.errorHandler());

desktopApp.get('/', function(req, res){ 
  res.send('Desktop Route') 
});

desktopApp.get('/mobile', function(req, res){ 
  // Because Express respects the order that you set up the middleware chain,
  // the mobileApp `/mobile` route gets first dibs to send a response or next()
  res.send('Inaccessible Desktop Route') 
});

desktopApp.get('/mobile/foobar', function(req, res){ 
  // When mobileApp can't find any suitable route matching this path, it gives
  // up, and desktopApp continues to pass the request down the middleware stack.
  // It ends up matching this route, where we send a response
  res.send('Desktop Route') 
});

http.createServer(desktopApp).listen(3000, function(){
  console.log('Listening on 3000');
});


// Results
$ curl localhost:3000/
Desktop Route

$ curl localhost:3000/mobile/
Mobile Route

Upvotes: 4

laconbass
laconbass

Reputation: 17795

See the vhost example on the express github repository.

You can have a "main" app, which routes the requests to one app or another. You should write a middleware to establish the conditions where one app or another are requested. express.vhost is a good example, but maybe you need other checks than the domain one.

main-app.js

(The file called to start the server.)

// load dependencies

var main = express();

main.use( express.vhost( 'mobile', require( './the-mobile-app' ) );
main.use( express.vhost( '*', require( './the-web-app' ) );

main.listen( /*...*/ )

the-mobile-app and the-web-app.js

var app = express();
//
// setup your application conf, middleware, and routes
//
module.exports = app;

Upvotes: 3

Related Questions