Giter Club home page Giter Club logo

koa-cache-proxy's Introduction

koa-cache-proxy

这个项目 fork 自koa-proxy,在保留原有 api 的基础上拓展了其他功能,原始 api 参考原文档,下文只描述改动的部分

安装

npm install koa-cache-proxy -S

新特性

  • � 使用 async/await 代替 generate,不再支持低版本 koa2 和 node
  • 增加 hook 功能对某些自定义 url 做响应
  • 增加 cache 功能,可将反向代理的资源缓存在内存中,减少反向代理的次数

用例

  • hook 功能
const proxy = require('koa-cache-proxy')
...
app.use(
  proxy({
    host: 'https://alicdn.com',
    match: /^\/example(\/)?/,
    hooks: [
      {
        path: '/example/LfkFyA2UCcsn8NIr', //随便起的一个url
        handle(ctx) {
          // do something
        },
      },
    ],
 })
)
...

使用 hook 功能, hook 中定义的链接只会在 match 匹配的前提下生效

  • cache 功能
const proxy = require('koa-cache-proxy')
...
app.use(
  proxy({
    host: 'https://alicdn.com',
    match: /^\/example(\/)?/,
    cache: true //缓存所有,
    // cache: /\.html$/ 缓存所有html结尾文件
    // cache(path) { return false } 动态决定是否缓存
 })
)
...

cache 缓存时会连同 response header 也一并缓存

新增属性

使用此插件,在 koa 的 ctx 中将新增以下属性

  • ctx.isCacheable

ctx.path 是否符合缓存规则

LICENSE

Licensed under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.