diff options
Diffstat (limited to 'common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples')
7 files changed, 310 insertions, 0 deletions
diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/movies/index.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/movies/index.js new file mode 100644 index 0000000..c3140fa --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/movies/index.js @@ -0,0 +1,132 @@ +var express = require('express'), + bodyParser = require('body-parser'), + methodOverride = require('method-override'), + mongoose = require('mongoose'), + morgan = require('morgan'), + restful = require('../../'); +var app = module.exports = express(); + +app.use(morgan('dev')); +app.use(bodyParser.urlencoded({'extended':'true'})); +app.use(bodyParser.json()); +app.use(bodyParser.json({type:'application/vnd.api+json'})); +app.use(methodOverride()); +app.set('view engine', 'jade'); + +app.mongoose = mongoose; // used for testing + +mongoose.connect("mongodb://localhost/movies_test"); + +var user = app.user = restful.model('users', mongoose.Schema({ + username: { type: 'string', required: true }, + pass_hash: { type: 'number', required: true } + })) + .methods(['get', 'post', 'put', 'delete']) + .before('get', function(req, res, next) { + req.body.limit = 1; + next() + }) + .removeOptions({ + sort: 'field -username' + }) + .includeSchema(false); + + +var movie = app.movie = restful.model("movies", mongoose.Schema({ + title: { type: 'string', required: true }, + year: { type: 'number', required: true }, + creator: {type: 'ObjectId', ref: 'users' }, + genre: {type: 'ObjectId', ref: 'genres'}, + comments: [{ + body: {type: 'String'}, + date: {type: 'Date'}, + author: { type: 'ObjectId', ref: 'users' } + }], + meta: { + productionco: 'string', + director: { type: 'ObjectId', ref: 'users' } + }, + secret: { type: 'string', select: false } + })); + +movie.methods([ + { + method: 'get', + before: noop, + after: noop + }, + 'post', + 'put', + 'delete']) + .updateOptions({ new: true }) + .route('recommend', function(req, res, next) { + res.locals.status_code = 200; + res.locals.bundle.recommend = 'called'; + next(); + }) + .route('anotherroute', function(req, res, next) { + res.writeHead(200, {'Content-Type': 'application/json' }); + res.write(JSON.stringify({ + anotherroute: "called" + })); + res.end(); // This ends the request and prevents any after filters from executing + }) + .route('athirdroute', { + handler: function(req, res, next) { + res.locals.status_code = 200; // Store response status code + res.locals.bundle = { + athirdroute: "called" // And response data + }; + next(); // Call *after* filters and then return the response + }, + methods: ['get', 'post'], + detail: true // Will mount the route at the detail endpoint /movies/:id/athirdroute + }) + .route('pshh', { + handler: [function(req, res, next) { + next(); + }, function(req, res, next) { + next(); + }, function(req, res, next) { + res.locals.status_code = 200; + res.locals.bundle = { + pshh: "called" + }; + next(); + }], + methods: ['get', 'post'] + }) + .before('post', noop) // before a POST, execute noop + .after('post', noop) + .before('put', noop) + .after('put', noop) + .after('recommend', after) + .after('athirdroute', after); + +var genre = app.genre = restful.model("genres", mongoose.Schema({ + name: { type: 'string', required: true } + })); +genre.methods(['get', 'put', 'delete']); +genre.shouldUseAtomicUpdate = false; + +var review = app.genre = restful.model("reviews", mongoose.Schema({ + body: { type: 'string', required: true }, + length: { type: Number, min: 0, required: true} // https://github.com/baugarten/node-restful/issues/116 + })); +review.methods(['get', 'put', 'delete']); + +user.register(app, '/users'); +movie.register(app, '/api/movies'); +genre.register(app, '/api/genres'); +review.register(app, '/api/reviews'); + +if (!module.parent) { + app.listen(3000); +} + +function noop(req, res, next) { next(); } +function after(req, res, next) { + res.locals.bundle.after = 'called'; + next(); +} + diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes.js new file mode 100644 index 0000000..57efc5b --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes.js @@ -0,0 +1,79 @@ +var express = require('express'), + bodyParser = require('body-parser'), + methodOverride = require('method-override'), + mongoose = require('mongoose'), + morgan = require('morgan'), + restful = require('../'); +// Make a new Express app +var app = module.exports = express(); + +// Connect to mongodb +mongoose.connect("mongodb://localhost/restful"); + +// Use middleware to parse POST data and use custom HTTP methods +app.use(morgan('dev')); +app.use(bodyParser.urlencoded({'extended':'true'})); +app.use(bodyParser.json()); +app.use(bodyParser.json({type:'application/vnd.api+json'})); +app.use(methodOverride()); + +var hashPassword = function(req, res, next) { + if (!req.body.password) + return next({ status: 400, err: "No password!" }); // We can also throw an error from a before route + req.body.password = bcrypt.hashSync(req.body.password, 10); // Using bcrypt + return next(); // Call the handler +} + +var sendEmail = function(req, res, next) { + // We can get the user from res.bundle and status code from res.status and + // trigger an error by calling next(err) or populate information that would otherwise be miggins + next(); // I'll just pass though +} + +var User = restful.model( "users", mongoose.Schema({ + username: 'string', + password_hash: 'string', + })) + .methods(['get', 'put', 'delete', { + method: 'post', + before: hashPassword, // Before we make run the default POST to create a user, we want to hash the password (implementation omitted) + after: sendEmail, // After we register them, we will send them a confirmation email + }]); + +User.register(app, '/user'); // Register the user model at the localhost:3000/user + +var validateUser = function(req, res, next) { + if (!req.body.creator) { + return next({ status: 400, err: "Notes need a creator" }); + } + User.Model.findById(req.body.creator, function(err, model) { + if (!model) return next(restful.objectNotFound()); + return next(); + }); +} + +var Note = restful.model("note", mongoose.Schema({ + title: { type: 'string', required: true}, + body: { type: 'string', required: true}, + creator: { type: 'ObjectId', ref: 'user', require: true}, + })) + .methods(['get', 'delete', { method: 'post', before: validateUser }, { method: 'put', before: validateUser }]); + +Note.register(app, '/note'); + +User.route("notes", { + handler: function(req, res, next, err, model) { // we get err and model parameters on detail routes (model being the one model that was found) + Note.Model.find({ creator: model._id }, function(err, list) { + if (err) return next({ status: 500, err: "Something went wrong" }); + //res.status is the status code + res.status = 200; + + // res.bundle is what is returned, serialized to JSON + res.bundle = list; + return next(); + }); + }, + detail: true, // detail routes operate on a single instance, i.e. /user/:id + methods: ['get'], // only respond to GET requests +}); +app.listen(3000); diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/index.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/index.js new file mode 100644 index 0000000..6299c33 --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/index.js @@ -0,0 +1,25 @@ +var express = require('express'), + bodyParser = require('body-parser'), + methodOverride = require('method-override'), + mongoose = require('mongoose'), + morgan = require('morgan'), + restful = require('../'); +var app = module.exports = express(); + +// Connect to mongodb -- used to store the models +mongoose.connect("mongodb://localhost/expressmvc"); + +app.use(morgan('dev')); +app.use(bodyParser.urlencoded({'extended':'true'})); +app.use(bodyParser.json()); +app.use(bodyParser.json({type:'application/vnd.api+json'})); +app.use(methodOverride()); + +// expose a list of models to register +var models = require('./models/index'); +models.forEach(function(model) { + console.log("Register " + model.resourceName); + model.register(app, '/' + model.resourceName); +}); + +app.listen(3000); diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/index.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/index.js new file mode 100644 index 0000000..203375e --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/index.js @@ -0,0 +1,11 @@ +var User = require('./user'), + Notes = require('./note'), + userroutes = require('../routes/user'); + +console.log(userroutes); +for (var route in userroutes) { + User.userroute(route, userroutes[route]); +} + +exports = module.exports = [User, Notes]; + diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/note.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/note.js new file mode 100644 index 0000000..b62861a --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/note.js @@ -0,0 +1,28 @@ +var restful = require('../../../'), + mongoose = require('mongoose'), + User = require('./user'); + +var validateUser = function(req, res, next) { + console.log("Add User"); + if (req.body.creator) { + User.Obj.findById(req.body.creator, function(err, model) { + if (!model) { + return next(restful.objectNotFound()); + } + next(); + }); + } + next(); // Required, so error later +} + +var Note = new restful.Model({ + title: "note", + methods: ['get', 'delete', { type: 'post', before: validateUser }, { type: 'put', before: validateUser }], + schema: mongoose.Schema({ + title: { type: 'string', required: true}, + body: { type: 'string', required: true}, + creator: { type: 'ObjectId', ref: 'user', require: true}, + }), +}); + +exports = module.exports = Note; diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/user.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/user.js new file mode 100644 index 0000000..384e318 --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/models/user.js @@ -0,0 +1,13 @@ +var restful = require('../../..'), + mongoose = require('mongoose'); + +var User = new restful.Model({ + title: 'user', + methods: ['get', 'post', 'put', 'delete'], + schema: mongoose.Schema({ + username: 'string', + password: 'string', + }), +}); + +exports = module.exports = User; diff --git a/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/routes/user.js b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/routes/user.js new file mode 100644 index 0000000..f9ad33e --- /dev/null +++ b/common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes/routes/user.js @@ -0,0 +1,22 @@ +var User = require('../models/user'), + Note = require('../models/note'); + +// Here we can add custom route endpoints +exports = module.exports = { + // an endpoint called notes + notes: { + handler: function(req, res, next, err, model) { + Note.Obj.find({ creator: model._id }, function(err, list) { + if (err) next({ status: 404 }); // Error handling + //res.status is the status code + res.status = 200; + + // res.bundle is what is returned + res.bundle = list; + next(); + }); + }, + detail: true, // detail makes sure we have one model to work on i.e. /user/:id/note is the uri + methods: ['get'], // only respond to GET requests + }, +} |