How to detect a device is pc or mobile from which request is made

This question already has an answer here:

  • What is the best way to detect a mobile device in jQuery? 49 answers

  • Yes, you can discern from user agent of request. Write the regex for that yourself or use a ready-made npm module device

    code reference:

    var device = require('device')
    app.get('/', function(req, res) {
        var ua = req.headers['user-agent'];
        if(device.is('phone')){
           console.log('device is phone');
        }else if(device.is('tablet')){
           console.log('device is tablet');
        }else  if(device.is('desktop')){
           console.log('device is desktop');
        }else {
           console.log('device is something else');
        }
    });
    
    链接地址: http://www.djcxy.com/p/84060.html

    上一篇: Javascript中的移动设备检测

    下一篇: 如何检测设备是从哪个PC或移动设备发出请求