npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-path-mapping

v0.1.0

Published

Koa middleware for redirect or path rewrite of requests. Support path rewrite of failed requests.

Downloads

3

Readme

koa-path-mapping

version downloads

Koa middleware for redirect or path rewrite of requests. Support path rewrite of failed requests. 用于对请求进行重定向或路径重写的 koa 中间件,支持对失败的请求进行路径重写。

允许对 404 请求进行路径重写,并回退到 next(),对于以下场景非常有用:

  • 使用 HTML5 History API 的单页应用和多页应用
  • 使用 webpack-dev-middleware 的开发服务器,例如 webpack-serve

起步

安装

npm install --save-dev koa-path-mapping

用法

const Koa = require('koa');
const pathMappingMiddleware = require('koa-path-mapping').pathMappingMiddleware;

const app = new Koa();
const options = {
    mapping: [
        { action: 'redirect', from: '^/$', to: '/login' },
        { action: 'rewrite', from: '^/game($|/)', to: '/app-game.html' },
    ],
    error: [{ action: 'rewrite', status: 404, to: '/app.html' }],
};
app.use(pathMappingMiddleware(options));

API

pathMappingMiddleware(options: PathMappingOptions)

PathMappingOptions

创建中间件的选项。

PathMappingOptions.mapping

类型: MappingOptions[] 默认: [] 参考

PathMappingOptions.error

类型: ErrorOptions[] 默认: [] 参考

PathMappingOptions.ignoreJson

类型: boolean 默认: true 是否忽略 application/json 的请求。

PathMappingOptions.enableLog

类型: boolean 默认: false 是否输出日志。

PathMappingOptions.logger

类型: Function 默认: console.log 设置输出日志所使用的函数。


MappingOptions

对请求进行重定向或路径重写,在进入时执行。

MappingOptions.action

类型: 'redirect' | 'rewrite' 设置执行的动作,必填。

MappingOptions.from

类型: RegExp | string 设置匹配的 path,如果未设置,则匹配所有的请求。值要求为 RegExp 实例,或是 RegExp 格式的字符串。

MappingOptions.to

类型: string 设置目标的 path,必填。


ErrorOptions

对失败的请求进行重定向或路径重写,在 next() 之后执行。如果匹配成功并执行路径重写,那么会再一次执行 next()

ErrorOptions.action

类型: 'redirect' | 'rewrite' 设置执行的动作,必填。

ErrorOptions.status

类型: number 设置匹配的状态码,如果未设置,则匹配 status >= 400

ErrorOptions.from

类型: RegExp | string 设置匹配的 path,如果未设置,则匹配所有的请求。值要求为 RegExp 实例,或是 RegExp 格式的字符串。

ErrorOptions.to

类型: string 设置目标的 path,必填。

License

MIT