koa-historify
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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

Package Sidebar

Install

npm i koa-historify

Weekly Downloads

325

Version

1.0.0

License

MIT

Unpacked Size

8.21 kB

Total Files

6

Last publish

Collaborators

  • choysen