Friday, 28 April 2017

User Agent Sniffing with Regex in Express has 50% failure rate?

I have a regex defined in my express router to figure out if the UA is mobile or not, and depending on if it is or not, render a different page.

Upon testing on our production site, it has a 25% - 50% failure rate.

I'd like to know the root cause and a fix.

Thank you.

Index.js

  const moment = require('moment');
        var _ = require('lodash');
        var mobileRegex = new RegExp(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i, 'ig');

    ......
    .....
    .....

    router.get('/', function(req, res, next) {

      if (mobileRegex.test(req.headers['user-agent']) == true) {
          console.log('User-Agent: ' + req.headers['user-agent']);
        res.render('index-mobile')
      } else {
        res.render('index');
      }
    });

    router.get('/([\*])', function(req,res, next) { //Escaping * because cloudflare page rules aren't built right
      res.redirect('/')
    });

etc...



via QueSo

No comments:

Post a Comment