1.0.0 • Published 3 years ago

koa-historify v1.0.0

Weekly downloads
9
License
MIT
Repository
github
Last release
3 years ago

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

1.0.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago