Express UA Redirect is a simple configurable middleware for ExpressJS allowing you to redirect all routes to a configured route based on user agent restrictions. It was create to warn the user that the browser is not compatible, but it can be used for other reasons.
Here is an example to redirect IE 8 and less user to /incompatible-browser
route:
app.use(uaRedirect({
browsers: {
unauthorized: {
IE: '8-'
}
},
redirectTo: '/incompatible-browser'
}));
There is also an evergreen mode to accept all automatic updated browsers and redirect all other:
app.use(uaRedirect({
browsers: {
evergreen: true
},
redirectTo: '/incompatible-browser'
}));
Install the dependency
npm install --save express-ua-redirect
Install it on your express server
var uaRedirect = require('express-ua-redirect');
//...
app.use(uaRedirect(options));
- Proceed to installation has describe before
- Configure the
browsers
option - Add a route to your server according to the option
redirectTo
- Make what you will with this route
- You're done!
To redirect IE7 to /update-your-browser
who it render update-your-browser.ejs
template.
var express = require('express');
var uaRedirect = require('express-ua-redirect');
var app = express();
app.set('view engine', 'ejs');
// Configure uaRedirect
app.use(uaRedirect({
browsers: {
unauthorized: {
IE: 7
}
},
redirectTo: '/update-your-browser'
}));
// ...
// Define your routes
// ...
// Define the associate route to `redirectTo` option
app.get('/update-your-browser', function(req, res) {
res.render('update-your-browser');
});
<h1>Oups your browser is not compatible</h1>
<p>We recommend that you update your browser ...</p>
- browsers {Object}
- unauthorized {Object} Specify unauthorized browser(s) and its version with a browser object described below
- authorized {Object} Specify authorized browser(s) and its version with a browser object described below
- evergreen {Boolean} Redirect all not evergreen browser
- redirectTo {String} Route path
{
IE: 8, // match the exact version of IE8
Chrome: '44+', // match chrome 44 and above
Safari: '8-' // match safari 8 and below
}
Browser name is not case sensitive an you can use all browser listed on ua-parser-js plugin documentation here
Version condition can be String
or Number
. With +
or -
indicator it must be a String
.
If you specify an unauthorized browser it will override authorized browser and authorized override evergreen browsers.
unauthorized > authorized > evergreen
app.use(uaRedirect({
browsers: {
unauthorized: {
IE: '8-'
}
},
redirectTo: '/incompatible-browser'
}));
app.use(uaRedirect({
browsers: {
authorized: {
safari: '7+',
IE: '9+'
},
evergreen: true
},
redirectTo: '/incompatible-browser'
}));