https://github.com/opentable/decorouter
Simple decorator based es6 class method routing for express nodejs
https://github.com/opentable/decorouter
Last synced: 6 months ago
JSON representation
Simple decorator based es6 class method routing for express nodejs
- Host: GitHub
- URL: https://github.com/opentable/decorouter
- Owner: opentable
- License: mit
- Archived: true
- Created: 2016-05-25T01:43:40.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-10T02:45:21.000Z (over 9 years ago)
- Last Synced: 2025-06-11T19:49:18.289Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 23
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Decorouter
===================
Simple decorator based es6 class method routing for express nodejs
[](http://badge.fury.io/js/decorouter)
[](https://travis-ci.org/decorouter/spur-ioc)
## Installation
```bash
$ npm install decorouter --save
```
## Usage
usage requiring [babel-plugin-transform-decorators-legacy](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy) with babel 6
```javascript
import { Route } from '../decorouter';
export class Controller
{
constructor() {
//usage not requiring babel-plugin-transform-decorators-legacy
Route('get','/methodWithoutDecorator')(this, 'methodWithoutDecorator');
}
//additional handlers can be added after route eg (req, res, next) => next()
@Route('get', '/method')
method(req, res) {
res.send({from: 'method'});
}
//async methods can (and probably should) be used where neccessary
@Route('get', '/methodAsync')
async methodAsync(req, res) {
res.send({from: 'methodAsync'});
}
//additional handlers can be added after route eg (req, res, next) => next()
@Route('get', '/methodWithAdditionalHandler', (req, res, next) => { res.handlerCalled = true; next();})
methodWithAdditionalHandler(req, res) {
res.send({
from: 'methodWithAdditionalHandler',
handlerCalled: res.handlerCalled});
}
methodWithoutDecorator(req, res) {
res.send({from: 'methodWithoutDecorator'});
}
//will defaut to 'get' and '/defaultRouteAssignedByMethodName'
@Route()
defaultRouteAssignedByMethodName(req, res) {
res.send({from: 'defaultRouteAssignedByMethodName'});
}
}
```
Registering routes
```javascript
import { addRoutes, addRoutesFromDir } from 'decorouter';
import express from 'express';
import { Controller } from './testControllers';
let router = express.Router();
addRoutes(router, () => new Controller());
//or all in this directory
addRoutesFromDir(router, module, (typeObject) => new typeObject());
```