@ntlab/express-controller
v1.0.0
Published
Nodejs Express controller
Downloads
4
Readme
Nodejs Express Controller
Nodejs Express Controller is a helper class to define routes in Expressjs.
Usage
Create a controller in controller/app.js
.
const Controller = require('@ntlab/express-controller');
class AppController extends Controller
{
buildRoutes() {
this.addRoute('index', 'get', '/', async (req, res, next) => {
res.render('app/main');
});
}
static create(app) {
const prefix = '/';
const controller = new AppController({prefix: prefix, name: 'App'});
app.use(prefix, controller.router);
return controller;
}
}
module.exports = AppController.create;
Register it in Expressjs app.
const app = express();
// controllers
const Controller = require('@ntlab/express-controller');
Controller.scan(path.join(__dirname, 'controller'), (controller, name) => {
controller(app);
});
// sub controllers
Controller.subControllers.forEach(controller => {
const mountPath = controller.getMountPath();
if (mountPath) {
app.use(mountPath, controller.router);
}
});