https://github.com/brookshi/webapi-router
create restful api with the same feeling of Asp.Net WebApi based on Koa
https://github.com/brookshi/webapi-router
koa nodejs router webapi webapi-router
Last synced: 21 days ago
JSON representation
create restful api with the same feeling of Asp.Net WebApi based on Koa
- Host: GitHub
- URL: https://github.com/brookshi/webapi-router
- Owner: brookshi
- License: apache-2.0
- Created: 2017-01-19T08:24:04.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-04T14:58:10.000Z (over 6 years ago)
- Last Synced: 2025-03-26T00:12:11.887Z (about 1 month ago)
- Topics: koa, nodejs, router, webapi, webapi-router
- Language: TypeScript
- Homepage:
- Size: 31.3 KB
- Stars: 16
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webapi-router
[](https://www.npmjs.com/package/webapi-router/)
create restful api application with the same feeling of Asp.Net WebApi
## Feature
Dont need write the annoying koa.use(...). Instead, webapi-router will auto use the controller file path for router path.Also, use decorator `@GET(path)` can indicate the router path directly.
Support `@GET`, `@POST`, `@DELETE`, `@PUT`, `@OPTIONS`, `@HEAD`, `@PATCH`.
## Usage
### 1. set folder of controllers and restful api prefix in index.ts/js.
```ts
import { WebApiRouter } from '../lib/index';app.use(new WebApiRouter().router('sample/controllers', 'api'));
```
### 2. add controller base on BaseController
```ts
export class TestController extends BaseController
{}
```
### 3. add decorator for function
```ts
///:name is a path param, get it in arguments by using @PathParam
/// get body by using @BodyParam
/// get query param by using @QueryParam
@POST('/user/:name') // argument is option, if empty, will using controller file path as router path.
postWithPathParam(@PathParam('name') name: string, @QueryParam('id') id: string, @BodyParam body: any) {
console.info(`TestController - post with name: ${name}, body: ${JSON.stringify(body)}`);
return 'ok';
}
```## That is all!!