使用带帆的 express-device [英] using express-device with sails

查看:28
本文介绍了使用带帆的 express-device的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在尝试使用风帆框架作为计划移植我的快递应用程序.我遇到的问题是如何制作 express-device npm 包来使用它.在快速应用程序中,我只需要像这种情况下的模块 var device = require('express-device') 然后在 server.configure 中配置它.有什么帮助吗?

been trying to play with sails framework as planning to port my express app. The problem I got into was how to make express-device npm package to work with it. In express app I just require module like in this case var device = require('express-device') and then configure it in server.configure. Any help?

推荐答案

好的,所以我找到了解决方案...运行 npm install express-device --save 后,我必须在 config/中创建一个 express.js 模块dir 和模块的内容如下所示:

Ok, so I found the solution... After running npm install express-device --save I had to create a express.js module inside of config/ dir and the content of module looks like this:

module.exports.express = {
  customMiddleware: function (app) {
    var device = require('express-device');
    app.use(device.capture());
  }
};

所以现在在每个控制器中,如果我需要,我可以找到请求来自的设备类型:

so now in each controller I can find the type of device that request is coming from if I need to:

index: function(req, res) {
  device = req.device.type;
  if(device == 'mobile') {
    // do something different ;)
  }
}

也许有一些更聪明的方法可以做到这一点,但这对我来说很好:)

Maybe there's some more clever ways to do this but this works fine for me :)

这篇关于使用带帆的 express-device的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆