Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miaowing/koa-router-decoration
https://github.com/miaowing/koa-router-decoration
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/miaowing/koa-router-decoration
- Owner: miaowing
- Created: 2017-06-06T17:44:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T08:15:53.000Z (over 7 years ago)
- Last Synced: 2024-04-26T04:21:33.597Z (7 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# koa-router-decoration
## Usage
```
npm install koa-router-decoration --save
npm install koa-validator-middleware --save
```### UserController.js
```javascript
import RouterDecorator, {
RequestMapping, GetMapping, PostMapping, DeleteMapping} from 'koa-router-decoration';
import {NotEmpty} from 'koa-validator-middleware';@RequestMapping('/users')
export default class UserController extends RouterDecorator {
constructor() {
super();
}
@GetMapping('/')
async getUsers(ctx) {
}
@GetMapping('/:userId')
async getUser(ctx) {
const userId = ctx.params.userId;
}
@PostMapping('/', NotEmpty('request.body.username'))
async createUser(ctx) {
if (!ctx.validation.pass) {
//@see https://github.com/miaowing/koa-validator-middleware for detail.
}
}
@DeleteMapping('/:userId')
async deleteUser(ctx) {
}
}
```### app.js
```javascript
import Koa from 'koa';
import KoaRouter from 'koa-router';import UserController from './UserController';
const app = new Koa();
const rootRouter = new KoaRouter();rootRouter.use(new UserController());
app.use(rootRouter.routes());
app.use(rootRouter.allowedMethods());app.start(3000);
````## API
### Route(path, method, ...middleware)
### RequestMapping(path, method, ...middleware)
The same as @Route.
### GetMapping(path, ...middleware)
### PostMapping(path, ...middleware)
### PutMapping(path, ...middleware)
### DeleteMapping(path, ...middleware)