express-simple-controllers
Create super simple controllers for routing an express app.
How to use
First off, initialize the controllers
;; const router = express; ;
Then create a controllers
directory and add anything with an _controller.js
suffix. e.g. home_controller.js
.
Anything with that suffix will be added as a controller.
If you need to change the controller directory...
;
Basic
You can create controller methods in a few different ways.
const show = method: "GET" route: "/home" { res; }; ;
Handler types
const show = method: "GET" route: "/home" handler: { res; } { res; } ; ;
Middleware and before
If you export a before
function, it will run before every method in the file unless you pass a skipBefore: true
.
const before = { reqsomething = "a thing"; ;} const show = method: "GET" route: "/home" { const something = req; res; }; const showNoBefore = method: "GET" route: "/home" skipBefore: true { // ... }; ;
You can also pass an array of of middleware.
const show = method: "GET" route: "/home" middleware: first { res; }; ;
CRUD routes
You can also create a simple crud controller by creating named functions in the exports...
// user_controller.jsconst before = ; // GET /userconst show = {}; // GET /usersconst list = {}; // PUT /userconst update = {}; // POST /userconst create = {}; // DELETE /userconst delete = {}; ;
The prefix of the file sets the route, in the above case /user
because the controller is named user_controller.js
.
Build
Will build to the /dist
directory.
npm run build
Running tests
npm run testnpm run test:ci # Runs in watch mode