express-common-controller

3.1.0 • Public • Published

Express Common Controller

This is a common controller for using express

Current Status:

NPM Version NPM Downloads Build Status

NPM

Installation

$ npm|yarn install express-common-controller

Usage

First step:

Create a controller inherit BaseController.

  • HelloController.js
const BaseController = require('express-common-controller').BaseController;
 
function HelloController() {}
 
HelloController.prototype = new BaseController();
 
HelloController.prototype.index = function() {
  this.render("Hello World");
};
 
module.exports = HelloController;

Or Using ES6 style

import { BaseController } from 'express-common-controller';
 
class HelloController extends BaseController {
  constructor() {
    super();
  }
 
  index() {
    this.render('Hello World');
  }
}
 
export default HelloController;

Second step:

Create a router config like this:

const path = require('path');
const ExpressCommonControllerRouter = require('express-common-controller').default;
 
const router = new ExpressCommonControllerRouter();
 
router.path = path.join(__dirname, './js/controllers');
 
router.get('/hello', 'HelloController#hello');
 
module.exports = router.routes();

NOTE ExpressCommonControllerRouter based on ExpressCommonRouter. More info please refer to here:express-common-router

Third step:

Using routes config in server.js

const express = require('express');
const routes = require('./routes');
const app = express();
 
app.use(routes);
 
app.listen(3000, '0.0.0.0', (err) => {
  if (err) {
    console.log(err);
    return;
  }
});

Config your routes.

This component support all methods which supported by express.

About the details of config route, please refer to here: Express Router

License

express-common-controller is released under the MIT license.

/express-common-controller/

    Package Sidebar

    Install

    npm i express-common-controller

    Weekly Downloads

    10

    Version

    3.1.0

    License

    MIT

    Last publish

    Collaborators

    • mixspa