ki

koa-ip

Ip filter middleware for koa, support whitelist and blacklist.

Showing:

Popularity

Downloads/wk

9K

GitHub Stars

37

Maintenance

Last Commit

1yr ago

Contributors

5

Package

Dependencies

3

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

koa-ip

KoaJs Slack

koa-ip is a ip filter middleware for koa, support whitelist and blacklist.

Install

$ npm i koa-ip --save
or 
$ yarn add koa-ip

Usage

ip(String|RegExp)
ip(Array{String|RegExp})
ip({
  whitelist: Array{String|RegExp},
  blacklist: Array{String|RegExp},
  handler: async (ctx, next) => {}// handle blacklist ip
})

Examples

const Koa = require('koa')
const ip = require('koa-ip')

const app = new Koa()

app.use(ip('192.168.0.*'))// whitelist
// app.use(ip(['192.168.0.*', '8.8.8.[0-3]']))// whitelist
// app.use(ip({
//   whitelist: ['192.168.0.*', '8.8.8.[0-3]'],
//   blacklist: ['144.144.*']
// }))

app.listen(3000)

blacklist handler

const app = new Koa()
app.use((ctx, next) => {
  ctx.request.ip = '127.0.0.1'
  return next()
})
app.use(ip({
  blacklist: ['127.0.0.*'],
  handler: async (ctx, next) => {
    ctx.status = 403
  }
}))

app.use((ctx, next) => {
  ctx.status = 200
})

app.listen(3000)

NB: If missing blacklist handler, default ctx.status = 403.

More examples see test.

Test

$ npm test (coverage 100%)
or
$ yarn test (coverage 100%)

License

MIT

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial