Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/capdilla/koa2-controller
library to handle the routes and controller at same time
https://github.com/capdilla/koa2-controller
controller koa koa2 koajs router
Last synced: about 2 months ago
JSON representation
library to handle the routes and controller at same time
- Host: GitHub
- URL: https://github.com/capdilla/koa2-controller
- Owner: capdilla
- License: mit
- Created: 2018-03-26T21:39:25.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-30T16:54:00.000Z (about 6 years ago)
- Last Synced: 2024-11-16T16:20:43.031Z (2 months ago)
- Topics: controller, koa, koa2, koajs, router
- Language: JavaScript
- Size: 48.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/capdilla/koa2-controller.svg?branch=master)](https://travis-ci.org/capdilla/koa2-controller)
# koa2-controller
library to handle the routes and controller at same time## Installation
```sh
npm i koa2-controller-router --save
```**Example**
Basic usage:
`of koa2Controller````javascript
const {koa2Controller} = require('koa2-controller-router')class testController extends koa2Controller {
//if you need to set parameters
constructor(props) {
super(props)
}paramsBehaviour() {
return {
postCreate: {
rules: [
{ name: 'mail', type: 'require' },// say to require this
{ name: 'password', type: 'require' },
]
},
}
}//create a http get
getHello(ctx, next) {
return ctx.body = "Hello world"
}//create a http post
postCreate(ctx) {
return ctx.body = "created"
}//create a http put
putUser(id, ctx) {
return ctx.body = `user id : ${id} , updated`
}//create a http del
delUser(id, ctx) {
return ctx.body = `user id : ${id} , deleted`
}/**
* if you are using koa-views you can use the method renderView
* to render a view that match, instead use ctx.render('folder/index.ejs') use ctx.render('index.ejs')
* your view have to be in a folder with the same name of the controller
* for example TestController the method match only Test in lower case
*/
allView(ctx) {
return ctx.renderView("index.ejs", { name: 'my awsome test' })
}}
```see test folder for more examples
Basic usage:
`of controllerRoutes`
this get all the files in `/app/controllers/` by default
```javascript
const Koa = require('koa');
const app = new Koa();//import this
const { controllerRoutes } = require('koa2-controller-router')
//find in the default path and set props to the routes
app.use(new controllerRoutes({props:{ db:'db conection',...etc }}).routes())//find in a diferent path and diferent prefix
const privateControllers = new controllerRoutes({
absolutePath: __dirname + '/app/controllers/private/',
prefix: '/v2'
})
app.use(privateControllers.routes())app.listen(3000, () => {
console.log("listen")
})
```
## Testing```sh
npm run test
```