【问题标题】:How to set parseUserAgent to true in express for express-device如何在 express 中为 express-device 将 parseUserAgent 设置为 true
【发布时间】:2017-01-29 20:01:32
【问题描述】:

我一直在研究 express-device,如果我只想要设备类型,我可以让它正常工作。

var device = require('express-device');

app.use(device.capture());

device.enableViewRouting(app);

router.get("/",function(req,res){
    res.json({"Message" : "Hello World !"});
    var accessDate;
    accessDate = new Date();

    console.log("Device Type: " +  req.device.type.toUpperCase() + " Device Name:" + req.device.description + " Date: "+ accessDate.toDateString());
    console.log(req.device);

})

我的 req.device 如下所示

{ parser: 
   DeviceParser {
    options: 
     {  emptyUserAgentDeviceType: 'desktop',
        unknownUserAgentDeviceType: 'phone',
        botUserAgentDeviceType: 'bot',
        carUserAgentDeviceType: 'car',
        parseUserAgent: false },
        make_sure_parser_was_executed: [Function],
        get_model: [Function],
        get_type: [Function] 
     }, 
  type: 'desktop',
  name: '' 
}

如果我想获取设备名称或描述,我需要将 parseUserAgent 设置为 true。我的问题是我该怎么做?也许这只是漫长的一周,我有点脑死亡,但我这辈子看不到你是怎么设置的。

【问题讨论】:

    标签: node.js express user-agent


    【解决方案1】:

    parseUserAgent 等设置可以通过在设置使用时将选项对象传递给中间件来设置:

    app.use(device.capture({
      parseUserAgent: true
    }));
    

    From the module's readme:

    从 0.3.4 版开始,您现在可以在调用 device.capture() 时覆盖一些选项。它接受一个只有配置选项的对象(与device 支持的相同),您可以覆盖[...]。那些你不覆盖它的将使用默认值。

    【讨论】:

      猜你喜欢
      • 2019-07-12
      • 2021-09-05
      • 2020-11-28
      • 2014-04-21
      • 2017-01-03
      • 2021-08-27
      • 1970-01-01
      • 2021-04-04
      • 2021-07-13
      相关资源
      最近更新 更多