Skip to content

Fallback to index.html for applications that are using the HTML 5 history API for Koa server

License

Notifications You must be signed in to change notification settings

dalhorinek/koa-history-api-fallback

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version 1.0.0 includes fix for koa v3.

koa-history-api-fallback

Koa implementation of [connect-history-api-fallback](https://github.com/bripkens/connect-history-api-fallback)

NPM

Introduction

This is simple rewrite of connect-history-api-fallback

Usage

The middleware is available through NPM and can easily be added.

npm install --save koa-history-api-fallback

Import the library

var historyApiFallback = require('koa-history-api-fallback');

var app = koa();

app.use(historyApiFallback());

Options

You can optionally pass options to the library when obtaining the middleware

var middleware = historyApiFallback({});

index

Override the index (default /index.html)

historyApiFallback({
  index: '/default.html'
});

rewrites

Override the index when the request url matches a regex pattern. You can either rewrite to a static string or use a function to transform the incoming request.

The following will rewrite a request that matches the /\/soccer/ pattern to /soccer.html.

historyApiFallback({
  rewrites: [
    { from: /\/soccer/, to: '/soccer.html'}
  ]
});

Alternatively functions can be used to have more control over the rewrite process. For instance, the following listing shows how requests to /libs/jquery/jquery.1.12.0.min.js and the like can be routed to ./bower_components/libs/jquery/jquery.1.12.0.min.js. You can also make use of this if you have an API version in the URL path.

historyApiFallback({
  rewrites: [
    {
      from: /^\/libs\/.*$/,
      to: function(context) {
        return '/bower_components' + context.parsedUrl.pathname;
      }
    }
  ]
});

The function will always be called with a context object that has the following properties:

  • parsedUrl: Information about the URL as provided by the URL module's url.parse.
  • match: An Array of matched results as provided by String.match(...).

verbose

This middleware does not log any information by default. If you wish to activate logging, then you can do so via the verbose option or by specifying a logger function.

historyApiFallback({
  verbose: true
});

Alternatively use your own logger

historyApiFallback({
  logger: console.log.bind(console)
});

About

Fallback to index.html for applications that are using the HTML 5 history API for Koa server

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%