Skip to content

Instantly share code, notes, and snippets.

@particlebanana
Created September 4, 2013 23:10
Show Gist options
  • Save particlebanana/6444048 to your computer and use it in GitHub Desktop.
Save particlebanana/6444048 to your computer and use it in GitHub Desktop.
// 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'
};
// Simple User model
// /api/models/User.js
module.exports = {
attributes: {
name: 'string'
}
};
// 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);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment