The express router used by Ike.
Ike Router inspires itself a lot on the Rails router and implements some of the basic concepts. You''ll get routes using shorthands, resources and some helpers.
Install using npm:
$ npm install --save ike-router
Controllers must be a class (not even a class instance). Ike router instantiates the class and executes a method within it's context.
// CONTROLLER
class SamplesController {
constructor() {}
index(req, res) {
let hello = this.helloWorld();
res.status(200).send(hello);
}
helloWorld() {
retunrn 'Hello world';
}
}
module.exports = SamplesController;
Having controllers like this will make you able to:
// MAIN APP FILE (OR WHATEVER YOU LIKE)
const express = require('express');
const app = express();
const routes = require('ike-router')();
routes.get('/', 'samples#index');
app.use('/', routes.draw());
app.listen(3000, () => { console.log('Example running') });
This will boot a simple server, routed by Ike router.
Any express-compatible middelware function can be passed to the route with the middleware
parameter. It can be a single function or array of functions. It will be executed in the order they are declared, leaving the controller function for last.
Passing middleware:
routes.get('/', 'samples#index', { middleware: someFunc })
Or multiple middlewares, to be executed in the declared order:
routes.get('/', 'samples#index', { middleware: [someFunc, otherFunc] })
You can choose the controllers path when you instantiate the routes object.
...
const routes = require('ike-router')('./controllers/');
...
It will be realtive to where the app.js file is being executed from.