Easy way to do controllers using express
With support to Async/Await and Generator based control flow
Steps to setup a controller
Create a new file to be a controller;
Import the library
const Controller = ;
- Instantiate the controller with its base path
const ctrl = '/' ;
- Append any filters you want
A filter is an abstraction for any route middleware used by the express, so it is a function with this signature:
Check out Express.js docs for more info.
- Define the routes (again, like plain express.js):
- Export the routes at the end of the files
moduleexports = ctrlroutes;
- Import this controller in your main app.js
const app = ;app;app;// ...app;// OR, if those are in a path:;
In the example,
walk is a library to get all files from a path, you can find here.
Wait there is more!
If you want to have
async methods inside one route logic, just do it:
- Generator based control flow
If you want to yield your code, with promises, inside a generator, this is supported too
This is done using
simplerunner library info here.
- Errors! Errors! and more Errors!
Once more, the error treatment is express-ish, just append this after your routes:
More info here.
This errors include exceptions and rejections threw by the yield/awaited functions as well.