Code Monkey home page Code Monkey logo

express-ua-redirect's Introduction

Express User Agent Redirect

Coverage Status

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'
}));

Installation

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));

How to use

  1. Proceed to installation has describe before
  2. Configure the browsers option
  3. Add a route to your server according to the option redirectTo
  4. Make what you will with this route
  5. You're done!

Exemple

To redirect IE7 to /update-your-browser who it render update-your-browser.ejs template.

server.js

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');
});

update-your-browser.ejs

<h1>Oups your browser is not compatible</h1>
<p>We recommend that you update your browser ...</p>

options

  • 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

Browser object possibilities

{
  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.

Priority order

If you specify an unauthorized browser it will override authorized browser and authorized override evergreen browsers.

unauthorized > authorized > evergreen

Use cases

Lock IE8 and below

app.use(uaRedirect({
  browsers: {
    unauthorized: {
      IE: '8-'
    }
  },
  redirectTo: '/incompatible-browser'
}));

Authorized modern browsers

app.use(uaRedirect({
  browsers: {
    authorized: {
      safari: '7+',
      IE: '9+'
    },
    evergreen: true
  },
  redirectTo: '/incompatible-browser'
}));

express-ua-redirect's People

Contributors

cledoc avatar hikkyu avatar piwaii avatar ramonpage avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

qrb ramonpage

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.