项目作者: CHOYSEN

项目描述 :
💫 HTML5 History-API middleware for Koa2
高级语言: TypeScript
项目地址: git://github.com/CHOYSEN/koa-historify.git
创建时间: 2020-10-01T06:36:51Z
项目社区:https://github.com/CHOYSEN/koa-historify

开源协议:MIT License

下载


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

  1. npm install koa-historify --save

OR

  1. yarn add koa-historify

Usage

  1. // ...
  2. const koaHistorify = require('koa-historify')
  3. const indexPath = path.join(
  4. __dirname,
  5. 'static/index.html' /* index.html filepath */
  6. )
  7. const app = new Koa()
  8. // ...
  9. // Ensure koa-historify is used after other middlewares, otherwise please use the `prepose` mode
  10. app.use(koaHistorify(indexPath))
  11. app.listen(80)

Options

logger

You can provide a function that can log the information

  1. app.use(
  2. koaHistorify(indexPath, {
  3. logger: console.log.bind(console)
  4. })
  5. )

prepose

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

  1. // ...
  2. const staticPath = path.join(__dirname, 'static')
  3. const indexPath = path.join(staticPath, 'index.html' /* index.html filepath */)
  4. const app = new Koa()
  5. app.use(
  6. koaHistorify(indexPath, {
  7. prepose: true
  8. })
  9. )
  10. app.use(koaStatic(staticPath))
  11. app.use(router.routes())
  12. // ...
  13. app.listen(80)

License

MIT