@ntlab/express-controller

1.0.0 • Public • Published

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);
    }
});

Readme

Keywords

Package Sidebar

Install

npm i @ntlab/express-controller

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

17.5 kB

Total Files

5

Last publish

Collaborators

  • tohenk