server-side browser detection? node.js

前端 未结 15 1084
不知归路
不知归路 2020-12-07 09:29

Most implementations i\'ve seen are for browser detection on the client side. I was just wondering if it was possible to do browser detection before sending any resources to

相关标签:
15条回答
  • 2020-12-07 09:54

    Try this code http://detectmobilebrowser.com/

    0 讨论(0)
  • 2020-12-07 09:55

    If you want to control mobile in the templating layer, I just wrote a module for that. https://github.com/Fresheyeball/isMobile-node

    0 讨论(0)
  • 2020-12-07 09:56

    [Here is another variation or assimilation for your consideration.]

    It is more versatile and simplified further.

    You can pass the Request or any object with a 'headers' property or it could be the headers property and you can pick any label to search for the parameter on the object or the headers or the actual user agent string itself.

    It used the previously posted Mobile and Table Checking Regex, and simply returns that result, but by first sanctifying the input, one can plug various things in.

    You can even override the default regex optionally passable as an argument. {I'll leave that further extension to the inspired.} Also one could have another way to default to the globally stored user-agent from the request if in scope etc.

    mobTabCheck: function( ua, lbl, rgx ) {  /* mobile tablet check; UserAgent or request, or any object with optional search label  */
        if( ua === und ) return false;
        if( ua !== und && ua.constructor !== String ) {
            if( lbl === und ) lbl = 'user-agent';
            if( ua.headers !== und ) ua = ua.headers[ lbl ];
            else ua = ua[ lbl ];
        }
        if( rgx === und ) rgx = /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile|Kindle|NetFront|Silk-Accelerated|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune/;
        if( rgx.constructor === String ) rgx = new RegExp( rgx );
        return rgx.test( ua );
    }
    

    This Regex came from here... https://gist.github.com/dalethedeveloper/1503252/931cc8b613aaa930ef92a4027916e6687d07feac

    The 98% Solution. I don't know if it checks tablets like my function title implies.

    Really the title of this function (and some arguments) should be rename maybe?... serachObjectForLabelThatMatchesThisRegex

    except all the defaults make it a single argument purposed thing.

    Also I leave the function set as a key's value, which you can store however you prefer like... just promise me no var or const if you use it.

    let mobTabCheck = function() {};
    
    0 讨论(0)
  • 2020-12-07 09:58

    You might want to have a look at Apache DeviceMap.

    JavaScript libraries out of the box are more on the client side right now, but much will work on Node.JS or Angular in a similar way. Unlike simple pattern matching of UA strings DeviceMap comes with a vast range of devices and device families in its Device Description Repository (DDR) based on W3C standards.

    0 讨论(0)
  • 2020-12-07 10:01

    if your using express you can easily check the ua with something like this:

    app.get('/ua', function(req, res){
        res.send('user ' + req.headers['user-agent']);
    });
    
    0 讨论(0)
  • 2020-12-07 10:02

    I threw this together using ua-parser-js. I'm sure it can be improved but it's functional.

    Install the package:

    sudo npm install ua-parser-js
    

    In your routes file require UAParser:

    var UAParser = require('ua-parser-js');
    

    Do some stuff with it:

    function ensureLatestBrowser(req, res, next) {
      var parser = new UAParser();
      var ua = req.headers['user-agent'];
      var browserName = parser.setUA(ua).getBrowser().name;
      var fullBrowserVersion = parser.setUA(ua).getBrowser().version;
      var browserVersion = fullBrowserVersion.split(".",1).toString();
      var browserVersionNumber = Number(browserVersion);
    
      if (browserName == 'IE' && browserVersion <= 9)
        res.redirect('/update/');
      else if (browserName == 'Firefox' && browserVersion <= 24)
        res.redirect('/update/');
      else if (browserName == 'Chrome' && browserVersion <= 29)
        res.redirect('/update/');
      else if (browserName == 'Canary' && browserVersion <= 32)
        res.redirect('/update/');
      else if (browserName == 'Safari' && browserVersion <= 5)
        res.redirect('/update/');
      else if (browserName == 'Opera' && browserVersion <= 16)
        res.redirect('/update/');
      else
        return next();
    }
    

    and then in your route just call:

    app.all(/^(?!(\/update)).*$/, ensureLatestBrowser);
    

    If you want to see what other information you can get with UAParser check out their demo page.

    0 讨论(0)
提交回复
热议问题