node.js - How to serve a different home page with Express? -


is possible serve different home page user depending if they're logged in or not?

for example, app uses:

app.use(express.static(__dirname + '/public')); 

which serves index.html home page.

although serve login.html home page if user not authenticated, , app.html if user authenticated. using middleware.

can done expressjs?

you can try following approach :

app.get('/', function(req, res, next) {      try {          var contents, authenticated;            // user logged-in or not.          authenticated = true/false;            if(!authenticated) {              contents = require('fs').readfilesync(require('path').join(__dirname, './login.html')).tostring();          }          else {              contents = require('fs').readfilesync(require('path').join(__dirname, './index.html')).tostring();          }                    res.setheader('content-type', 'text/html');          res.send(new buffer(contents));      } catch(ex) {          res.setheader('content-type', 'application/json');          res.send(json.stringify(ex));      }  });


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo