Last active
August 29, 2015 14:20
-
-
Save zhiephie/1af5cf2edead8ee57f51 to your computer and use it in GitHub Desktop.
MeteorRest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello = new Mongo.Collection('hello'); | |
if(Meteor.isServer) { | |
// Global API configuration | |
Restivus.configure({ | |
useAuth: true, | |
prettyJson: true | |
}); | |
// Generates: GET, POST, DELETE on /api/hello and GET, PUT, DELETE on | |
// /api/hello/:id for Hello collection | |
Restivus.addCollection(Hello); | |
// Generates: GET, POST on /api/users and GET, DELETE /api/users/:id for | |
// Meteor.users collection | |
Restivus.addCollection(Meteor.users, { | |
excludedEndpoints: ['deleteAll', 'put'], | |
routeOptions: { | |
authRequired: true | |
}, | |
endpoints: { | |
post: { | |
authRequired: false | |
}, | |
delete: { | |
roleRequired: 'admin' | |
} | |
} | |
}); | |
// Maps to: /api/posts/:id | |
Restivus.addRoute('posts/:id', {authRequired: true}, { | |
get: function () { | |
var post = Posts.findOne(this.urlParams.id); | |
if (post) { | |
return {status: 'success', data: post}; | |
} | |
return { | |
statusCode: 404, | |
body: {status: 'fail', message: 'Post not found'} | |
}; | |
}, | |
post: { | |
roleRequired: ['author', 'admin'], | |
action: function () { | |
var post = Posts.findOne(this.urlParams.id); | |
if (post) { | |
return {status: "success", data: post}; | |
} | |
return { | |
statusCode: 400, | |
body: {status: "fail", message: "Unable to add post"} | |
}; | |
} | |
}, | |
delete: { | |
roleRequired: 'admin', | |
action: function () { | |
if (Posts.remove(this.urlParams.id)) { | |
return {status: "success", data: {message: "Item removed"}}; | |
} | |
return { | |
statusCode: 404, | |
body: {status: "fail", message: "Item not found"} | |
}; | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment