koa2-router
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -170,3 +170,3 @@ /*! | ||
var idx = 0; | ||
var protohost = getProtohost(ctx.url) || '' | ||
var protohost = getProtohost(ctx.url) || ''; | ||
var removed = ''; | ||
@@ -646,3 +646,3 @@ var slashAdded = false; | ||
// send an OPTIONS response | ||
async function sendOptionsResponse(ctx, options) { | ||
function sendOptionsResponse(ctx, options) { | ||
var body = options.join(','); | ||
@@ -649,0 +649,0 @@ ctx.set('Allow', body); |
@@ -57,11 +57,5 @@ /*! | ||
Layer.prototype.handle = function handle(ctx, next) { | ||
Layer.prototype.handle = async function handle(ctx, next) { | ||
var fn = this.handler; | ||
// promisify the handle result | ||
try { | ||
return Promise.resolve(fn(ctx, next)); | ||
} catch(e) { | ||
return Promise.reject(e); | ||
} | ||
await fn(ctx, next); | ||
}; | ||
@@ -68,0 +62,0 @@ |
{ | ||
"name": "koa2-router", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "A express-liked router component for koa2", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# koa2-router | ||
A express-liked router component for koa2 | ||
An express-liked router component for koa2 | ||
[![NPM Version][npm-image]][npm-url] | ||
## Getting Started | ||
You can follow the instructions below to setup a router componnent in koa@2 environment. | ||
You can follow the instructions below to setup a router component in koa@2 environment. | ||
@@ -41,3 +43,3 @@ ### Prerequisties | ||
* use http method handle request | ||
* Use http method to handle request | ||
``` | ||
@@ -47,2 +49,34 @@ router2.get('/:userId', ctx => ctx.body = `hello user ${ctx.params.userId}`); | ||
* Use params middleware like express | ||
``` | ||
const router3 = Router(); | ||
router3.params('userName', (ctx, next, userName, key) => (ctx[key] = userName, next())) | ||
.get('/:userName', async ctx => ctx.body = await ctx.db.getStaffFromName(ctx.userName)); | ||
router.use('/staff', router3); | ||
``` | ||
* Use route to make a rest api | ||
``` | ||
const route = router3.route('/:id'); | ||
route | ||
.get(async ctx => ctx.body = await ctx.db.getStaffFromId(ctx.params.id)); | ||
``` | ||
* exit route or router without exception | ||
``` | ||
route | ||
.all(async (ctx, next) => { | ||
if (ctx.authenticate.userId === ctx.params.id) return next(); | ||
else throw 'route'; // exit this route without any exception | ||
}) | ||
.put(async ctx => ctx.body = await ctx.db.updateStaff(ctx.params.id, ctx.request.body)) | ||
.del(async ctx => ctx.body = await ctx.db.deleteStaff(ctx.params.id)); | ||
route3.use('/admin', (ctx, next) => { | ||
if (ctx.authenticate.userRoles.includes('admin')) return next(); | ||
else throw 'router'; // exit this router3 without any exception | ||
}) | ||
.post('/posts', async ctx => ctx.body = await ctx.db.createPost(ctx.request.body, ctx.authenticate.userId)); | ||
``` | ||
## Running tests | ||
@@ -54,7 +88,10 @@ You should clone thie repository down to your file system, and execute | ||
## License | ||
This project is licensed under the MIT License | ||
## Acknowledgements | ||
* Thanks to the [expressjs/express](https://github.com/expressjs/express) project | ||
* Thanks to the [koa-router](https://github.com/alexmingoia/koa-router) project | ||
## License | ||
[MIT](LICENSE) | ||
[npm-image]: https://img.shields.io/npm/v/koa2-router.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa2-router |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26384
95
827