Tuesday 15 March 2011

node.js - Two apps in expressjs -


I am creating an app with Express JS which will have separate clients like Web and Mobile. Because some middleware did not want to use an app because some middleware would be extra burdens. To say like session middleware, is it possible that a project has two apps and how will it work?

app object that you do in Express, a function (Rick, Race , The next) which is suitable for express midwire series. Therefore, you can use the app.use to send requests that match the main path segment for the defined app elsewhere.

docs:

  $ npm installs express //mobile.js var requires app = ('Express') (); App.get ('/', function (rik, ridge) {res.send ('mobile root'}}); Module.exports = app; //desktopApp.js var http = Required ('http'); Var Express = Required ('Express'); Var Desktop app = express (); Var requires mobile app = ('./ mobile.js'); DesktopApp.use ('/ mobile', mobileApp) desktopApp.use (desktopApp.router); DesktopApp.use (express.errorHandler ()); DesktopApp.get ('/', function (rick, ridge) {res.send ('desktop route'}}; DesktopApp.get ('/ mobile', function (req, res) {// Express because you Respects order to establish series, // mobileapp `/ mobile` route to send a response first or next) Res.send ('missing desktop root')}); DesktopApp.get ('/ mobile / foobar', function (req, res) {// When the MobileApp can not find a suitable route to match this route, it gives / / above, and the desktop app middleware stack Continues to pass the request below. // This corresponds to this route, where we send a response res.send ('Desktop Routes')}; Http.createServer (desktopApp) .listen (3000, function) { Console.log (listening on '3000);}); // Results $ curl localhost: 3000 / desktop root $ curl localhost: 3000 / mobile / mobile root    

No comments:

Post a Comment