Skip to content

Instantly share code, notes, and snippets.

@esco
Created May 19, 2014 13:32
Show Gist options
  • Save esco/036ed9e2e374fa268e3b to your computer and use it in GitHub Desktop.
Save esco/036ed9e2e374fa268e3b to your computer and use it in GitHub Desktop.
{
"name": "express-device",
"author": {
"name": "Rodrigo Guerreiro",
"url": "@rguerreiro"
},
"description": "Browser detection library, built on top of express",
"keywords": [
"browser",
"mobile",
"detection",
"user-agent",
"express",
"layout",
"view routing",
"desktop",
"phone",
"tablet",
"tv",
"bot",
"responsive design"
],
"version": "0.3.9",
"main": "./index.js",
"private": false,
"engines": {
"node": "*"
},
"dependencies": {
"express": ">=3.0.0",
"express-partials": ">=0.1.1"
},
"devDependencies": {
"ejs": "*",
"mocha": "*",
"supertest": "*"
},
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/rguerreiro/express-device"
},
"readme": "# express-device [![Build Status](https://secure.travis-ci.org/rguerreiro/express-device.png?branch=master)](http://travis-ci.org/rguerreiro/express-device) [![NPM version](https://badge.fury.io/js/express-device.svg)](http://badge.fury.io/js/express-device)\r\n\r\n## why express-device?\r\n\r\nI'm really into node.js and lately I've been playing a lot with it. One of the steps I wanted to take in my learning path was to build a node.js module and published it to npm. \r\n\r\nThen I had an idea: why not develop a server side library to mimic the behaviour that Twitter's [Bootstrap](http://twitter.github.com/bootstrap/scaffolding.html#responsive) has in order to identify where a browser is running on a desktop, tablet or phone. This is great for a [responsive design](http://en.wikipedia.org/wiki/Responsive_Web_Design), but on the server side.\r\n\r\n## how it came to be?\r\n\r\nFirst I started to search how I could parse the user-agent string and how to differentiate tablets from smartphones. I found a couple good references, such as:\r\n- http://www.useragentstring.com/pages/useragentstring.php\r\n- http://detectmobilebrowsers.com/\r\n- http://www.quirksmode.org/js/detect.html\r\n- http://googlewebmastercentral.blogspot.pt/2011/03/mo-better-to-also-detect-mobile-user.html\r\n- http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3888106/How-Can-I-Detect-the-iPhone--iPads-User-Agent.htm\r\n- http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/08/29/introducing-the-ie9-on-windows-phone-mango-user-agent-string.aspx\r\n\r\nBut then I came across with Brett Jankord's [blog](http://www.brettjankord.com). He developed [Categorizr](http://www.brettjankord.com/2012/01/16/categorizr-a-modern-device-detection-script/) which is what I was trying to do for node.js but for PHP. He has the code hosted at [Github](https://github.com/bjankord/Categorizr). So, express-device parsing methods (to extract device type) are based on Categorizr. Also I've used all of his [user-agent strings compilation](http://brettjankord.com/categorizr/categorizr-results.php) to build my unit tests.\r\n\r\n## how to use it?\r\n\r\nTo install it you only need to do:\r\n \r\n $ npm install express-device\r\n \r\nCase you're using [express](http://expressjs.com/) 2.x.x you should install version 0.1.2:\r\n \r\n $ npm install [email protected]\r\n\r\nexpress-device is built on top of [express](http://expressjs.com/) framework. Here's an example on how to configure express to use it:\r\n\r\n```javascript\r\napp.configure(function(){\r\n app.set('view engine', 'ejs');\r\n app.set('view options', { layout: false });\r\n app.set('views', __dirname + '/views');\r\n \r\n app.use(express.bodyParser());\r\n app.use(device.capture());\r\n});\r\n```\r\nBy doing this you're enabling the **request** object to have a property called **device**, which have the following properties:\r\n<table>\r\n <tr><td><strong>Name</strong></td><td><strong>Field Type</strong></td><td><strong>Description</strong></td><td><strong>Possible Values</strong></td></tr>\r\n <tr>\r\n <td>type</td>\r\n <td>string</td>\r\n <td>It gets the device type for the current request</td>\r\n <td>desktop, tv, tablet, phone or bot</td>\r\n </tr>\r\n</table>\r\n\r\nSince version 0.3.4 you can now override some options when calling **device.capture()**. It accepts an object with only the config options you which to override (go [here](https://github.com/rguerreiro/express-device/blob/master/test/not_default_options_test.js) for some examples). The ones you don't override it will use the default values. Here's the list with the available config options:\r\n<table>\r\n <tr><td><strong>Name</strong></td><td><strong>Field Type</strong></td><td><strong>Description</strong></td><td><strong>Possible Values</strong></td></tr>\r\n <tr>\r\n <td>emptyUserAgentDeviceType</td>\r\n <td>string</td>\r\n <td>Device type to be returned whenever the request has an empty user-agent. Defaults to phone.</td>\r\n <td>desktop, tv, tablet, phone or bot</td>\r\n </tr>\r\n\t<tr>\r\n <td>unknownUserAgentDeviceType</td>\r\n <td>string</td>\r\n <td>Device type to be returned whenever the request user-agent is unknown. Defaults to desktop.</td>\r\n <td>desktop, tv, tablet, phone or bot</td>\r\n </tr>\r\n <tr>\r\n <td>botUserAgentDeviceType</td>\r\n <td>string</td>\r\n <td>Device type to be returned whenever the request user-agent belongs to a bot. Defaults to bot.</td>\r\n <td>desktop, tv, tablet, phone or bot</td>\r\n </tr>\r\n</table>\r\n\r\nexpress-device can also add some variables to the response [locals property](http://expressjs.com/api.html#res.locals\\(\\)) that will help you to build a responsive design:\r\n<table>\r\n <tr>\r\n <td>is_desktop</td>\r\n <td>It returns true in case the device type is \"desktop\"; false otherwise</td>\r\n </tr>\r\n <tr>\r\n <td>is_phone</td>\r\n <td>It returns true in case the device type is \"phone\"; false otherwise</td>\r\n </tr>\r\n <tr>\r\n <td>is_tablet</td>\r\n <td>It returns true in case the device type is \"tablet\"; false otherwise</td>\r\n </tr>\r\n <tr>\r\n <td>is_mobile</td>\r\n <td>It returns true in case the device type is \"phone\" or \"tablet\"; false otherwise</td>\r\n </tr>\r\n <tr>\r\n <td>is_tv</td>\r\n <td>It returns true in case the device type is \"tv\"; false otherwise</td>\r\n </tr>\r\n <tr>\r\n <td>is_bot</td>\r\n <td>It returns true in case the device type is \"bot\"; false otherwise</td>\r\n </tr>\r\n <tr>\r\n <td>device_type</td>\r\n <td>It returns the device type string parsed from the request</td>\r\n </tr>\r\n</table>\r\nIn order to enable this methods you have to call **app.enableDeviceHelpers()**, just before **app.use(app.router)**.\r\n\r\nHere's an example on how to use them (using [EJS](https://github.com/visionmedia/ejs) view engine):\r\n```html\r\n<html>\r\n<head>\r\n <title><%= title %></title>\r\n</head>\r\n<body>\r\n <h1>Hello World!</h1>\r\n <% if (is_desktop) { %>\r\n <p>You're using a desktop</p>\r\n <% } %>\r\n <% if (is_phone) { %>\r\n <p>You're using a phone</p>\r\n <% } %>\r\n <% if (is_tablet) { %>\r\n <p>You're using a tablet</p>\r\n <% } %>\r\n <% if (is_tv) { %>\r\n <p>You're using a tablet</p>\r\n <% } %>\r\n <% if (is_bot) { %>\r\n <p>You're using a tablet</p>\r\n <% } %>\r\n</body>\r\n</html>\r\n```\r\n\r\nYou can check a full working example [here](https://github.com/rguerreiro/express-device/tree/master/example).\r\n\r\nIn version 0.3.0 a cool feature was added: the ability to route to a specific view\\layout based on the device type (must call the **app.enableViewRouting()** to set it up). Consider the code below:\r\n\r\n .\r\n |-- views\r\n |-- phone\r\n | |-- layout.ejs\r\n | `-- index.ejs\r\n |-- layout.ejs\r\n `-- index.ejs\r\n\r\nAnd this code:\r\n```javascript\r\napp.configure(function(){\r\n app.set('view engine', 'ejs');\r\n app.set('view options', { layout: true });\r\n app.set('views', __dirname + '/views');\r\n \r\n app.use(express.bodyParser());\r\n app.use(device.capture());\r\n\r\n app.enableViewRouting();\r\n});\r\n\r\napp.get('/', function(req, res) {\r\n res.render('index.ejs');\r\n})\r\n```\r\n\r\nIf the request comes from a *phone* device then the response will render views/phone/index.ejs view with views/phone/layout.ejs as layout. If it comes from another type of device then it will render the default views/index.ejs with the default views/index.ejs. Simply add a folder below your views root with the device type code (phone, tablet, tv or desktop) for the device type overrides. Several combinations are supported. Please check the [tests](https://github.com/rguerreiro/express-device/blob/master/test/view_route_test.js) for more examples.\r\n\r\nYou also have an **ignore** option:\r\n```javascript\r\napp.get('/', function(req, res) {\r\n res.render('index.ejs', { ignoreViewRouting: true });\r\n})\r\n```\r\n\r\nView routing feature uses the **express-partials** module for layout detection. If you would like to turn it off, you can use the **noPartials** option (be advised that by doing this you can no longer use the master\\partial layout built into express-device, but you can route to full views):\r\n```javascript\r\napp.configure(function(){\r\n app.set('view engine', 'ejs');\r\n app.set('view options', { layout: true });\r\n app.set('views', __dirname + '/views');\r\n \r\n app.use(express.bodyParser());\r\n app.use(device.capture());\r\n\r\n app.enableViewRouting({\r\n \t\"noPartials\":true\r\n });\r\n});\r\n\r\napp.get('/', function(req, res) {\r\n res.render('index.ejs');\r\n})\r\n```\r\n\r\n\r\n## contributors\r\n\r\n- [@rguerreiro](https://github.com/rguerreiro)\r\n- [@aledbf](https://github.com/aledbf)\r\n- [@ryansully](https://github.com/ryansully)\r\n- [@lyxsus](https://github.com/lyxsus)\r\n- [@dsyph3r](https://github.com/dsyph3r)\r\n- [@davo11122](https://github.com/davo11122)\r\n\r\n## where to go from here?\r\n\r\nCurrently express-device is on version 0.3.9. There are a couple of things that I have in mind to add, such as:\r\n- trying to parse the OS from the request\r\n\r\nFeel free to add issues with your own ideas or make pull requests (prefered method).\r\n\r\n## license\r\n\r\n(The MIT License)\r\n\r\nCopyright (c) 2012 Rodrigo Guerreiro\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining\r\na copy of this software and associated documentation files (the\r\n'Software'), to deal in the Software without restriction, including\r\nwithout limitation the rights to use, copy, modify, merge, publish,\r\ndistribute, sublicense, and/or sell copies of the Software, and to\r\npermit persons to whom the Software is furnished to do so, subject to\r\nthe following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be\r\nincluded in all copies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\r\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\r\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\r\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\r\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r\n",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/rguerreiro/express-device/issues"
},
"homepage": "https://github.com/rguerreiro/express-device",
"_id": "[email protected]",
"dist": {
"shasum": "d7133e88dca7fbaa2dfba7c428873a3b643aa6f5"
},
"_from": "express-device@",
"_resolved": "http://registry.npmjs.org/express-device/-/express-device-0.3.9.tgz"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment