I want to do a mobile detection in express, make mobile and PC module totally independent, but keep the URL same, which means I cannot redirect a path to /mobile/... just because of the request is from mobile device.
Here is what do in app.js:
app.use(function(req, res){
var ua = req.header('user-agent');
if(/mobile/i.test(ua)) {
console.log('user agent is mobile');
mobileRouter.set(app);
} else{
console.log('user agent is pc');
pcRouter.set(app);
}
})
And in pcRouter.js:
var index = require('./pc/index');
var user = require('./pc/user');
var auth = require('./pc/auth');
module.exports.set = function(app) {
console.log('define pc router');
app.use('/', index);
app.use('/user', user);
app.use('/auth', auth);
}
In pc/index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
console.log('pc index');
res.render('index', { title: 'PC' });
});
module.exports = router;
When I request the home page, terminal only shows these two logs: 'user agent is pc ' and 'define pc router'
pc/index.js is not being executed, I guess it's because router must be defined at the app.js been started, but how can I realize what I said before:
Same URL route by user agent detection, and keep the mobile sub app & pc sub app totally independent