This is the routing module for the Specla Framework. Its a wrapper on top of the express router and it enables you to specify a path to the folder where your controllers are located. The goal of this module is to make it easier to include your controllers for your application.
npm install specla-router --save
const Router = require('specla-router');
const express = require('express');
const app = express();
const router = new Router(app, {
path: __dirname+'/api/controllers'
});
// basic route to a controller action
router.get('/', 'Controller.action');
// route with middleware
router.get('/authenticated', [authenticated], 'Controller.action');
app.listen(3000);
The example below is the boilerplate code for a Controller. The class contuctor will work as a middleware function.
class Controller {
constructor(req, res){
// Do something before the action is triggered
}
action(req, res){
res.send('Controller action!');
}
}
module.exports = Controller;