diff options
author | 2017-08-07 11:39:39 +0800 | |
---|---|---|
committer | 2017-08-07 11:39:39 +0800 | |
commit | 21d72c4a80fe2937d0c4ddd20624b27adbcd989b (patch) | |
tree | e5013ee12f74f8452e01cbff16e7b0158bc456cb /common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes.js | |
parent | f533e73e2ae32e010b16abdcf7985abaf31ab843 (diff) |
Upload the ESR GUI seed code
Issue-ID: AAI-68
Change-Id: Ia50ce0570c2fabecd77199d4e8454f56fe587c4e
Signed-off-by: lizi00164331 <li.zi30@zte.com.cn>
Diffstat (limited to 'common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes.js')
-rw-r--r-- | common/src/main/webapp/usageguide/appserver/node_modules/node-restful/examples/notes.js | 79 |
1 files changed, 79 insertions, 0 deletions
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); |