Code Monkey home page Code Monkey logo

koa2-origin-cors's Introduction

cors middleware for koa2-server

带有域名拦截功能的koa2-cors中间件

Features

  • 黑名单域名模式
  • 白名单域名模式
  • 全局允许模式 (允许所有请求跨域并携带Credentials)
  • 基本跨域请求支持

Example

 const Koa = require('koa')
 const cors = require('koa2-origin-cors')  
 
 const server = new Koa()
 server.use(cors())

模式说明

allowAll

允许所有跨域请求并允许其携带Credentials凭据

server.use(cors({allowAll: true}))

blackList

blackList中的域名将被阻止访问

const corsOptoins = {
    blackList: [
        'test.dev.org', '123.22.11.33'
    ],
    terminationCode: 404 // [可选]拦截时返回Status403
    
}
server.use(corsOptions)

whiteList

只有白名单中的域名允许访问 注意本地非服务器模式访问时whiteList中添加String(null)

const corsOptions = {
    whiteList: [
        '192.168.1.123', 'null'
    ],
    terminationCode: 403 // [可选]拦截时返回Status
}
server.use(corsOptions)

origin

设置允许哪个域名下的访问跨域

server.user({
    origin: '192.168.1.110'
})

功能参数说明

  • allowAll {Boolean} 允许所有站点跨域并携带凭据
  • blackList {Array} 黑名单列表 黑名单中的域名将被拒绝访问
  • whiteList {Array} 白名单列表 只有白名单中的域名允许访问
  • origin {String | Function} 请求头Origin
  • credentials {Boolean} 允许携带凭据
  • terminationCode {Number} 拒绝返回httpStatus
  • vary {Boolean} 是否强制不同网站使用不同缓存 (a.git, b.git)

拦截优先级 allowAll > blackList > whiteList > origin



说明

初衷是想做一个域名拦截中间件,但与跨域设置使用2个中间件略显累赘,且与cors中有多处相似判断逻辑,所以把两部分功能整合到一起

整合了一部分koa2-cors 的源码

koa2-origin-cors's People

Contributors

republix avatar

Watchers

 avatar

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.