HTML5 History-API middleware for Koa2
English | 中文
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.
npm install koa-historify --save
OR
yarn add koa-historify
// ...
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)
You can provide a function that can log the information
app.use(
koaHistorify(indexPath, {
logger: console.log.bind(console)
})
)
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)