Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could have a controller class, where its constructor accepts an express object, setting up routes for you. So this is an example base <code>Controller</code> class:</p> <pre><code>/** * @param connect can either be Sencha Labs' `connect` module, or */ function Controller(express) { var self = this; var name = '/' + this._name; express.post(name, function (req, res, next) { self._create(req, res, next); }); express.get(name, function (req, res, next) { self._read(req, res, next); }); express.put(name + '/:id', function (req, res, next) { self._update(req, res, next); }); express.delete(name + '/:id', function (req, res, next) { self._delete(req, res, next); }); } // Since there aren't any protected variables in JavaScript, use // underscores to tell other programmers that `name` is protected. `name` // (or, more technically, `_name`) is still accessible, but at least, if a // team is disciplined enough, they'd know better than to access variables // with underscores in them. Controller.prototype._name = ''; Controller.prototype._create = function (req, res, next) { }; Controller.prototype._read = function (req, res, next) { }; Controller.protoype._update = function (req, res, next) { }; Controller.prototype._delete = function (req, res, next) { }; </code></pre> <p>And then, you can create a <code>Users</code> controller by extending from the <code>Controller</code> "class":</p> <pre><code>function UsersController(express) { Controller.call(this, express); } // This is not the most perfect way to implement inheritance in JavaScript, // this is one of the many ways. UsersController.prototype = Controller.prototype; UsersController.prototype._name = 'users' // An example override of the base `Controller#create` method. UsersController.prototype._create = function (req, res, next) { db.save(req.body, function (err) { if (err) res.send(500, err.message); res.redirect('/'); }); }; UsersController.prototype._read = function (req, res, next) { db.read(function (err, users) { if (err) res.send(500, err.message); res.send(users); }); }; </code></pre> <p>Once you have all the appropriate controllers declared and defined, you can start implementing them onto your express app.</p> <pre><code>// Initialize a new instance of your controller. var usersController = new UsersController(app); </code></pre> <p>P.S.: For the express calls in the constructor, there is another way to add your <code>create</code>, <code>read</code>, <code>update</code>, <code>delete</code> routes (and any other routes). I just didn't want to confuse you at first.</p> <pre><code>function Controller(express) { var name = '/' + this._name; express.post(name, this._create.bind(this)); express.get(name, this._read.bind(this)); express.put([name , ':id'].join('/'), this._update.bind(this)); express.delete([name, ':id'].join('/'), this._delete.bind(this)); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload