Browser detection library, built on top of express
npm install express-devicedevice package (https://www.npmjs.com/package/device) which was refactored from express-device for that purpose.
express-device only works with express >= v4.x.x and node >= v0.10. To install it you only need to do:
express-device is built on top of express framework. Here's an example on how to configure express to use it:
javascript
var device = require('express-device');
app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.set('views', __dirname + '/views');
app.use(bodyParser());
app.use(device.capture());
`
By doing this you're enabling the request object to have a property called device, which have the following properties:
Name Field Type Description Possible Values
type
string
It gets the device type for the current request
desktop, tv, tablet, phone, bot or car
name
string
It gets the device name for the current request
Example: iPhone. If the option parseUserAgent is set to false, then it will return an empty string
Since version 0.3.4 you can now override some options when calling device.capture(). It accepts an object with only the config options (the same that the device supports) you which to override (go here for some examples). The ones you don't override it will use the default values. Here's the list with the available config options:
Name Field Type Description Possible Values
emptyUserAgentDeviceType
string
Device type to be returned whenever the request has an empty user-agent. Defaults to desktop.
desktop, tv, tablet, phone, bot or car
unknownUserAgentDeviceType
string
Device type to be returned whenever the request user-agent is unknown. Defaults to phone.
desktop, tv, tablet, phone, bot or car
botUserAgentDeviceType
string
Device type to be returned whenever the request user-agent belongs to a bot. Defaults to bot.
desktop, tv, tablet, phone, bot or car
carUserAgentDeviceType
string
Device type to be returned whenever the request user-agent belongs to a car. Defaults to car.
desktop, tv, tablet, phone, bot or car
parseUserAgent
string
Configuration to parse the user-agent string using the useragent npm package. It's needed in order to get the device name. Defaults to false.
true | false
express-device can also add some variables to the response locals property) that will help you to build a responsive design:
is_desktop
It returns true in case the device type is "desktop"; false otherwise
is_phone
It returns true in case the device type is "phone"; false otherwise
is_tablet
It returns true in case the device type is "tablet"; false otherwise
is_mobile
It returns true in case the device type is "phone" or "tablet"; false otherwise
is_tv
It returns true in case the device type is "tv"; false otherwise
is_bot
It returns true in case the device type is "bot"; false otherwise
is_car
It returns true in case the device type is "car"; false otherwise
device_type
It returns the device type string parsed from the request
device_name
It returns the device name string parsed from the request
In order to enable this method you have to pass the app reference to device.enableDeviceHelpers(app), just before app.use(app.router).
Here's an example on how to use them (using EJS view engine):
`html
<%= title %>
Hello World!
<% if (is_desktop) { %>
You're using a desktop
<% } %>
<% if (is_phone) { %>
You're using a phone
<% } %>
<% if (is_tablet) { %>
You're using a tablet
<% } %>
<% if (is_tv) { %>
You're using a tv
<% } %>
<% if (is_bot) { %>
You're using a bot
<% } %>
<% if (is_car) { %>
You're using a car
<% } %>
`
You can check a full working example here.
In version 0.3.0 a cool feature was added: the ability to route to a specific view\layout based on the device type (you must pass the app reference to device.enableViewRouting(app) to set it up). Consider the code below:
.
|-- views
|-- phone
| |-- layout.ejs
| -- index.ejs
-- index.ejs
And this code:
`javascript
var device = require('express-device');
app.set('view engine', 'ejs');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');
app.use(bodyParser());
app.use(device.capture());
device.enableViewRouting(app);
app.get('/', function(req, res) {
res.render('index.ejs');
})
`
If 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 for more examples.
You also have an ignore option:
`javascript
app.get('/', function(req, res) {
res.render('index.ejs', { ignoreViewRouting: true });
})
`
There's a way to force a certain type of device in a specific request. In the example I'm forcing a desktop type and the view rendering engine will ignore the parsed type and render as if it was a desktop that made the request. You can use all the supported device types.
`javascript
app.get('/', function(req, res) {
res.render('index.ejs', { forceType: 'desktop' });
})
`
View 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):
`javascript
var device = require('express-device');
app.set('view engine', 'ejs');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');
app.use(express.bodyParser());
app.use(device.capture());
device.enableViewRouting(app, {
"noPartials":true
});
app.get('/', function(req, res) {
res.render('index.ejs');
})
`
contributors
- @rguerreiro
- @aledbf
- @ryansully
- @lyxsus
- @dsyph3r
- @davo11122
- @esco
- @Saicheg
- @brycekahle
- @manjeshpv
- @Sitebase
- @lennym
where to go from here?
Currently express-device` is on version 0.4.2. In order to add more features I'm asking anyone to contribute with some ideas. If you have some of your own please feel free to mention it here.