Skip to content

Instantly share code, notes, and snippets.

@particlebanana
Created September 4, 2013 23:10
Show Gist options
  • Select an option

  • Save particlebanana/6444048 to your computer and use it in GitHub Desktop.

Select an option

Save particlebanana/6444048 to your computer and use it in GitHub Desktop.

Revisions

  1. particlebanana created this gist Sep 4, 2013.
    7 changes: 7 additions & 0 deletions User.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    // Simple User model
    // /api/models/User.js
    module.exports = {
    attributes: {
    name: 'string'
    }
    };
    24 changes: 24 additions & 0 deletions UserController.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // Simple JSON controller method without error handling
    // /api/controllers/UserController.js
    module.exports = {

    index: function(req, res) {
    User.find().exec(function(err, users) {
    res.json(users);
    });
    },

    show: function(req, res) {
    var id = req.param('id');
    User.findOne(id).exec(function(err, user) {
    res.json(user);
    });
    },

    create: function(req, res) {
    User.create(req.params.all()).exec(function(err, user) {
    res.json(user);
    });
    }

    };
    23 changes: 23 additions & 0 deletions routes.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // Basic Sails Router Example
    // config/routes.js
    module.exports.routes = {

    'get /users': {
    controller: 'user',
    action: 'index'
    },

    'get /users/:id': {
    controller: 'user',
    action: 'show'
    },

    'post /users': {
    controller: 'user',
    action: 'create'
    },

    // You could also use the following syntax
    'put /users/:id': 'UserController.update'

    };