anil
anil

Reputation: 863

How to redirect user based on device in node js

I am having one issue for redirecting user dynamically based on device in node js application. My scenario is like this ==> If a user try to access my node js application from a mobile device, then i need to redirect user to res.render('/themes/v1/home'). other wise need to redirect user to res.render('/themes/v2/home'). I thought of using res.locals but res.locals is only to be used in views not inside of js files which are having code to redirect user. Kindly Help us.

Thanks in advance.

Upvotes: 0

Views: 118

Answers (1)

FastTurtle
FastTurtle

Reputation: 1787

You can do something like

app.get('/',function(req, res, next){
        var MobileDetect = require('mobile-detect'),
        md = new MobileDetect(req.headers['user-agent']);
        if(md.mobile()){
           res.render('/themes/v1/home'); // this renders view to redirect use res.redirect()
        } else {
             res.render('/themes/v2/home');
        }
     })      

hope it helps :)

Upvotes: 1

Related Questions