express-ctrlr

    0.0.3 • Public • Published

    express-ctrlr

    Build Status Code Climate David DM

    Controllers for express.js (4.x.x)

    Install

    npm install express-ctrlr
    

    Usage

    Standard CRUD

    var express = require('express');
    var controller = require('express-ctrlr');
    var app = express();
    
    var postsCtrl = controller()
      .index(function(req, res, next) { ... })
      .new(function(req, res, next) { ... })
      .create(function(req, res, next) { ... })
      .show(function(req, res, next) { ... })
      .edit(function(req, res, next) { ... })
      .update(function(req, res, next) { ... })
      .patch(function(req, res, next) { ... })
      .destroy(function(req, res, next) { ... });
    
    app.use('/posts', postsCtrl.router());
    

    Maps routes

    GET    /posts
    GET    /posts/new
    POST   /posts
    GET    /posts/:id
    GET    /posts/:id/edit
    PUT    /posts/:id
    PATCH  /posts/:id
    DELETE /posts/:id
    

    Define your own actions

    var postsCtrl = controller()
      .action("/new", function(req, res, next) { ... });
    
    app.use('/posts', postsCtrl.router());
    
    // GET /posts/new
    

    action defaults to the GET method. You can define the method VERB of choice.

    var postsCtrl = controller()
      .action("/", {method: 'POST'}, function(req, res, next) { ... });
    
    app.use('/posts', postsCtrl.router());
    
    // POST /posts
    

    Before actions

    var postsCtrl = controller()
      .before(isAuthenticated())
      .before(csrf())
      .new(function(req, res, next) { ... })
      .create(function(req, res, next) { ... });
    
    app.use('/posts', postsCtrl.router());
    
    // /posts/* isAuthenticated()
    // /posts/* csrf()
    // GET  /posts/new
    // POST /posts
    

    Order matters, kind of...

    var postsCtrl = controller()
      .create(function(req, res, next) { ... })
      .new(function(req, res, next) { ... })
      .before(isAuthenticated())
      .action("/", function(req, res, next) { ... })
      .before(csrf());
    
    app.use('/posts', postsCtrl.router());
    

    Will map in this order

    // /posts/* isAuthenticated()
    // /posts/* csrf()
    // GET  /posts
    // GET  /posts/new
    // POST /posts
    

    The mapping order is always

    1 before(s)               (then in the order they were defined)
    2 action(s)               (then in the order they were defined)
    3 <built in crud methods> (this order: index, new, create, show, edit, update, patch, destroy)
    

    Built in crud method order

    1 index
    2 new
    3 create
    4 show
    5 edit
    6 update
    7 patch
    8 destroy
    

    The built in crud methods will always maintain their defined map order regardless of their definition order.

    .show()
    .new()
    

    Will still map as

    // GET /posts/new
    // GET /posts/:id
    

    Alternate path definition.

    var ctrlr = controller()
      .new(...)
      .show(...)
      .create(...);
    
    app.use(ctrlr.router("/posts"));
    
    // GET  /posts/new
    // GET  /posts/:id
    // POST /posts
    

    This method allows for a simple namespacing pattern.

    app.use("/api", postsCtrlr.router("/posts"));
    app.use("/api", tasksCtrlr.router("/tasks"));
    
    // /api/posts/...
    // /api/tasks/...
    

    Nested routes.

    var ctrlr = controller()
      .show(...);
    
    app.use(ctrlr.router("/posts/:post_id/comments"));
    
    // GET /posts/:post_id/comments/:id
    

    Requires the path to be passed as the router argument in order to get all the URL params.

    License

    MIT

    Install

    npm i express-ctrlr

    DownloadsWeekly Downloads

    0

    Version

    0.0.3

    License

    MIT

    Last publish

    Collaborators

    • yunghwakwon