koa-historify icon indicating copy to clipboard operation
koa-historify copied to clipboard

πŸ’« HTML5 History-API middleware for Koa2

koa-historify πŸ’«

NPM version License

HTML5 History-API middleware for Koa2

English | δΈ­ζ–‡

Why

Unlike the current solution that relies on koa-static fallback, this project uses the idea of "default routing" to redirect unprocessed GET requests to index.html. This solution results in fewer configuration items and a more intuitive way.

Installation

npm install koa-historify --save

OR

yarn add koa-historify

Usage

// ...
const koaHistorify = require('koa-historify')
const indexPath = path.join(
  __dirname,
  'static/index.html' /* index.html filepath */
)

const app = new Koa()
// ...
// Ensure koa-historify is used after other middlewares, otherwise please use the `prepose` mode
app.use(koaHistorify(indexPath))
app.listen(80)

Options

logger

You can provide a function that can log the information

app.use(
  koaHistorify(indexPath, {
    logger: console.log.bind(console)
  })
)

prepose

It can be used before other middleware is used when prepose mode

// ...
const staticPath = path.join(__dirname, 'static')
const indexPath = path.join(staticPath, 'index.html' /* index.html filepath */)

const app = new Koa()
app.use(
  koaHistorify(indexPath, {
    prepose: true
  })
)
app.use(koaStatic(staticPath))
app.use(router.routes())
// ...
app.listen(80)

License

MIT