Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/midgarjs/route
Router systeme
https://github.com/midgarjs/route
Last synced: 20 days ago
JSON representation
Router systeme
- Host: GitHub
- URL: https://github.com/midgarjs/route
- Owner: midgarjs
- License: mit
- Created: 2020-06-23T17:19:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T03:03:12.000Z (almost 2 years ago)
- Last Synced: 2024-11-29T03:11:58.718Z (about 1 month ago)
- Language: JavaScript
- Size: 1.05 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://drone.midgar.io/api/badges/Midgar/controller/status.svg)](https://drone.midgar.io/Midgar/controller)
[![Coverage](https://sonar.midgar.io/api/project_badges/measure?project=midgar-controller&metric=coverage)](https://sonar.midgar.io/dashboard?id=midgar-controller)## @midgar/route
Plugin [Midgar](https://github.com/midgarjs/midgar) pour la gestion des controllers
## Installation
```sh
$ npm i @midgar/route
```Si tout s'est bien passé, un message de confirmation s'affiche:
```sh
#midgar-cli
@midgar/route added to plugins.json !
```## Fonctionnement
Ce plugin ajoute un type de module **midgar-controller** dans le dossier ./controllers/
Il suffit d'ajouter un controller dans le dossier ./controllers de votre plugin pour qu'il soit automatiquement importé au lancement de l'application et injecté dans express.
## Exemple de controller
```js
import { Controller } from '@midgar/route'// Tableau de service a injécter
// @see: https://github.com/midgarjs/service
const dependencies = ['mid:user']class UserController extends Controller {
// Les dépendances sont injécté dans le contructeur
constructor(mid, userService) {
super(mid)
this.prefix = 'user'
this.userService = userService
}/**
* Cette méthod est appelé automatiquement
* lors de l'instanciation du controller.
*/
init() {
this.addRoutes([
{
path: 'login',
action: (...args) => this.login(...args)
},
{
method: 'post',
path: 'login',
action: (...args) => this.loginPost(...args)
},
{
path: 'register',
action: (...args) => this.register(...args)
},
{
path: '/',
action: (...args) => this.users(...args)
}
])
}// GET /user/login route
async login(req, res) {
if (await this.userService.login(req.getParam('login'), req.getParam('password', false))) {
res.send({ success: true })
} else {
res.send({ success: false })
}
}// POST /user/login route
loginPost(req, res) {}// GET /user/register route
register(req, res) {}// GET /user route
user(req, res) {}
}export default {
dependencies,
controller: UserController
}
```Les methodes terminant par Route, GetRoute, PostRoute, AllRoute sont automatiquement transformer en routes.
[documentation Api](https://midgarjs.github.io/controller/).