Type Controllers
Allows to create controllers classes that handle requests. Simple wrapper over express.js routing.
Usage
Simply create a class and put annotations on its methods:
;; // and you need to require controllers and register actions in express app: ; require'./BlogController'; // require your controller; // create express applicationapp.usebodyParser.json; // setup body parserdefaultActionRegistry.registerActionsapp; // register actions in the appapp.listen3000; // run express app
If you want to include the directory with controllers do this:
;; ControllerUtils.requireAll; // includes all controllers; // create express applicationapp.usebodyParser.json; // setup body parserdefaultActionRegistry.registerActionsapp; // register actions in the appapp.listen3000; // run express app
You can also return result right from the controller, and this result will be pushed via response.send()
getAllrequest: Request, response: Response // its the same as:// request.send([{ id: 1, name: 'first blog' }, { id: 2, name: 'second blog' }]);
You can also return a Promise (object that contains .then method), and its result will be pushed to response.send after promise is resolved.
getAllrequest: Request, response: Response // its the same as SomeService.loadData().then(// result => request.send(result),// error => { request.status(500); request.send(error) }// );
You can inject some common express params you may need right into the controller action method:
getAll request: Request, response: Response, @Body requestBody: any, @Param'id' id: number, @QueryParam'name' name: string // its the same as: // let requestBody = response.body;// let id = response.params.id;// let name = response.query.name;
Take a look on samples in ./sample
for more examples of usages.
Todos
- cover with tests
- integration with other frameworks (other then express.js) can be easily added, so PRs are welcomed