博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RESTful Mongodb
阅读量:6306 次
发布时间:2019-06-22

本文共 20644 字,大约阅读时间需要 68 分钟。

Ref:

Ref:

Ref:

Ref:

Ref:

Ref:

Ref:

Ref:

Ref: 

Ref:

Ref:

{    "name": "node-api",    "main": "server.js",    "dependencies": {        "express": "~4.0.0",        "mongoose": "~3.6.13",        "body-parser": "~1.0.1"    }}
package.json
// app/models/beacon.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var BeaconSchema       = new Schema({    major        :Number,    minor        :Number,    range        :String//[I,N,F]});module.exports = mongoose.model('Beacon', BeaconSchema);
beacon.js
// app/models/catalog.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var CatalogSchema   = new Schema({    tag            :String,    name        :String,    updateDate    :Date});module.exports = mongoose.model('Catalog', CatalogSchema);
catalog.js
// app/models/group.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var GroupSchema   = new Schema({    tag            :String,    name        :String,    updateDate    :Date,    things        :[{ type: Schema.Types.ObjectId, ref: 'Things' }]});module.exports = mongoose.model('Group', GroupSchema);
group.js
// app/models/post.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var PostSchema    = new Schema({    title        :String,    description    :String,    html        :String,    photo        :[String],    audio        :[String],    video        :[String],    updateBy    :{ type: Schema.Types.ObjectId, ref: 'User' },    updateDate    :Date,    things        :{ type: Schema.Types.ObjectId, ref: 'Things' }});module.exports = mongoose.model('Post', PostSchema);
post.js
// app/models/things.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var BeaconSchema       = new Schema({    major        :Number,    minor        :Number,    range        :String//[I,N,F]});var CommentSchema    = new Schema({    text        :String,    html        :String,    photo        :String,    audio        :String,    video        :String,    doodle        :String,    type         :String,//[T,H,P,A,V,D]    createBy    :{ type: Schema.Types.ObjectId, ref: 'User' },    createDate    :Date});var ThingsSchema       = new Schema({    catalog        :{ type: Schema.Types.ObjectId, ref: 'Catalog' },    name        :String,    photo        :String,    description    :String,    contactInfo    :String,    type         :String,    subType        :String,    keyWord        :[String],    owner        :{ type: Schema.Types.ObjectId, ref: 'User' },    createDate    :Date,    audioInfo    :[String],    beacons        :[BeaconSchema],    comments    :[CommentSchema]});module.exports = mongoose.model('Things', ThingsSchema);
things.js
// app/models/type.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var TypeSchema     = new Schema({    tag     :String,    name    :String,    subType    :[TypeSchema]});module.exports = mongoose.model('Type', TypeSchema);
type.js
// app/models/user.jsvar mongoose     = require('mongoose');var Schema       = mongoose.Schema;var UserSchema   = new Schema({    firstname    :String,    lastname    :String,    nickname    :String,    password    :String,    gender        :String,//[F,M]    email        :String,    photo        :String,//base64    wechat        :{},    facebook    :{}});module.exports = mongoose.model('User', UserSchema);
user.js
// server.js// BASE SETUP// =============================================================================// call the packages we needvar express    = require('express');        // call expressvar app        = express();                 // define our app using expressvar bodyParser = require('body-parser');var mongoose   = require('mongoose');mongoose.connect('mongodb://192.168.0.202:27017/webuzz'); // connect to our database// configure app to use bodyParser()// this will let us get the data from a POSTapp.use(bodyParser.urlencoded({ extended: true }));app.use(bodyParser.json());var port = process.env.PORT || 2397;        // set our port// ROUTES FOR OUR API// =============================================================================var router = express.Router();              // get an instance of the express Router// middleware to use for all requestsrouter.use(function(req, res, next) {    // do logging    console.log('Something is happening.');    next(); // make sure we go to the next routes and don't stop here});// test route to make sure everything is working (accessed at GET http://localhost:2397/api)router.get('/', function(req, res) {    res.json({ message: 'hooray! welcome to our api!' });   });// model schema// -------------------------------------------------var User          = require('./app/models/user');var Catalog      = require('./app/models/catalog');var Beacon       = require('./app/models/beacon');var Post      = require('./app/models/post');var Things       = require('./app/models/things'); var Group        = require('./app/models/group'); var Type        = require('./app/models/type'); // -------------------------------------------------// more routes for our API will happen here// on routes that end in /users// ----------------------------------------------------router.route('/users')    // create a user (accessed at POST http://localhost:2397/api/users)    .post(function(req, res) {                var user = new User();      // create a new instance of the User model        user.nickname     = req.body.nickname;  // set the user info (comes from the request)        user.firstname     = req.body.firstname;         user.lastname     = req.body.lastname;         user.password   = req.body.password;        user.gender     = req.body.gender;         user.email         = req.body.email;         user.photo      = req.body.photo;        user.wechat     = req.body.wechat;         user.facebook     = req.body.facebook;         // save the user and check for errors        user.save(function(err) {            if (err)                res.send(err);            res.json({ message: 'User created!' });        });            })    // get all the users (accessed at GET http://localhost:2397/api/users)    .get(function(req, res) {        User.find(function(err, users) {            if (err)                res.send(err);            res.json(users);        });    });//find by emailrouter.route('/users/login/:email')    .get(function(req, res) {        User.find({
'email':req.params.email}, function(err, user) { if (err) res.send(err); res.json(user); }); });//find by email & passwordrouter.route('/users/login/:email/:password') .get(function(req, res) { User.findOne({ 'email' : req.params.email, 'password' : req.params.password }, function(err, user) { if (err) res.send(err); res.json(user); }); });//find by facebookrouter.route('/users/facebook/:id') .get(function(req, res) { User.findOne({
'facebook.id':req.params.id}, function(err, user) { if (err) res.send(err); res.json(user); }); });//find by wechatrouter.route('/users/wechat/:id') .get(function(req, res) { User.findOne({
'wechat.id':req.params.id}, function(err, user) { if (err) res.send(err); res.json(user); }); });// on routes that end in /users/:id// ----------------------------------------------------router.route('/users/:id') // get the user with that id (accessed at GET http://localhost:2397/api/users/:id) .get(function(req, res) { User.findById(req.params.id, function(err, user) { if (err) res.send(err); res.json(user); }); }) // update the user with this id (accessed at PUT http://localhost:2397/api/users/:id) .put(function(req, res) { // use our user model to find the user we want User.findById(req.params.id, function(err, user) { if (err) res.send(err); user.nickname = req.body.nickname; // update the user info user.firstname = req.body.firstname; user.lastname = req.body.lastname; user.password = req.body.password; user.gender = req.body.gender; user.email = req.body.email; user.photo = req.body.photo; user.wechat = req.body.wechat; user.facebook = req.body.facebook; // save the user user.save(function(err) { if (err) res.send(err); res.json({ message: 'User updated!' }); }); }); }) // delete the user with this id (accessed at DELETE http://localhost:2397/api/users/:id) .delete(function(req, res) { User.remove({ _id: req.params.id }, function(err, user) { if (err) res.send(err); res.json({ message: 'Successfully deleted' }); }); });router.route('/catalogs') // create a catalog (accessed at POST http://localhost:2397/api/catalogs) .post(function(req, res) { var catalog = new Catalog(); catalog.tag = req.body.tag; catalog.name = req.body.name; catalog.updateDate = req.body.updateDate; catalog.save(function(err) { if (err) res.send(err); res.json({ message: 'Catalog created!' }); }); }) // get all the catalogs (accessed at GET http://localhost:2397/api/catalogs) .get(function(req, res) { Catalog.find(function(err, catalogs) { if (err) res.send(err); res.json(catalogs); }); });router.route('/catalogs/:id') .get(function(req, res) { Catalog.findById(req.params.id, function(err, catalog) { if (err) res.send(err); res.json(catalog); }); }) .put(function(req, res) { Catalog.findById(req.params.id, function(err, catalog) { if (err) res.send(err); catalog.tag = req.body.tag; catalog.name = req.body.name; catalog.updateDate = req.body.updateDate; catalog.save(function(err) { if (err) res.send(err); res.json({ message: 'Catalog updated!' }); }); }); }) .delete(function(req, res) { Catalog.remove({ _id: req.params.id }, function(err, catalog) { if (err) res.send(err); res.json({ message: 'Successfully deleted' }); }); }); router.route('/things/addcomment/:id') .post(function(req, res) { Things.findById(req.params.id,function(err, things) { if (err) res.send(err); things.comments.push({ "text" : req.body.text, "html" : req.body.html, "photo" : req.body.photo, "audio" : req.body.audio, "video" : req.body.video, "doodle" : req.body.doodle, "type" : req.body.type, "createBy" : req.body.createBy, "createDate" : new Date(), }); Things.update({_id:things._id},{ $set: {comments: things.comments} },function(err){ if (err) res.send(err); }); res.json({ message: 'Comments update!' }); }); });router.route('/things') // create a things (accessed at POST http://localhost:2397/api/things) .post(function(req, res) { var things = new Things(); things.catalog = req.body.catalog; things.name = req.body.name; things.photo = req.body.photo; things.description = req.body.description; things.contactInfo = req.body.contactInfo; things.type = req.body.type; things.subType = req.body.subType; things.keyWord = req.body.keyWord; things.owner = req.body.owner; things.createDate = req.body.createDate; things.audioInfo = req.body.audioInfo; things.beacons = req.body.beacons; things.comments = req.body.comments; things.save(function(err) { if (err) res.send(err); res.json({ message: 'Things created!' }); }); }) // get all the things (accessed at GET http://localhost:2397/api/things) .get(function(req, res) { /* Things.find(function(err, things) { if (err) res.send(err); res.json(things); });*/ Things.find() .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); });router.route('/things/:id')/* .get(function(req, res) { Things.findById(req.params.id, function(err, things) { if (err) res.send(err); res.json(things); }); })*/ .get(function(req, res) { Things.findById(req.params.id) .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); }) .put(function(req, res) { Things.findById(req.params.id, function(err, things) { if (err) res.send(err); things.catalog = req.body.catalog; things.name = req.body.name; things.photo = req.body.photo; things.description = req.body.description; things.contactInfo = req.body.contactInfo; things.type = req.body.type; things.subType = req.body.subType; things.keyWord = req.body.keyWord; things.owner = req.body.owner; things.createDate = req.body.createDate; things.audioInfo = req.body.audioInfo; things.beacons = req.body.beacons; things.comments = req.body.comments; things.save(function(err) { if (err) res.send(err); res.json({ message: 'Things updated!' }); }); }); }) .delete(function(req, res) { Things.remove({ _id: req.params.id }, function(err, things) { if (err) res.send(err); res.json({ message: 'Successfully deleted' }); }); });//filter catalogrouter.route('/things/catalog/:id') .get(function(req, res) { Things.find({
'catalog':req.params.id}) /* .populate({ path : 'catalog', match : {tag : req.params.tag} })*/ .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); });//filter ownerrouter.route('/things/owner/:id') .get(function(req, res) { Things.find({ 'owner':req.params.id }) .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); });//filter typerouter.route('/things/type/:type') .get(function(req, res) { Things.find({ 'type':req.params.type }) .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); });//filter type & sub typerouter.route('/things/type/:type/:subType') .get(function(req, res) { Things.find({ 'type':req.params.type, 'subType':req.params.subType }) .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); });//filter key wordsrouter.route('/things/keyword/:key') .get(function(req, res) { Things.find({ 'keyWord':{"$in":[req.params.key]} }) .populate('catalog') .populate('owner') .populate('comments.createBy') .exec(function(err,things){ if (err) res.send(err); res.json(things); }); });router.route('/posts') // create a posts (accessed at POST http://localhost:2397/api/posts) .post(function(req, res) { var posts = new Post(); posts.title = req.body.title; posts.description = req.body.description; posts.html = req.body.html; posts.photo = req.body.photo; posts.audio = req.body.audio; posts.video = req.body.video; posts.updateBy = req.body.updateBy; posts.updateDate = req.body.updateDate; posts.things = req.body.things; posts.save(function(err) { if (err) res.send(err); res.json({ message: 'Post created!' }); }); }) // get all the posts (accessed at GET http://localhost:2397/api/posts) .get(function(req, res) { Post.find(function(err, posts) { if (err) res.send(err); res.json(posts); }); });router.route('/posts/:id') .get(function(req, res) { Post.findById(req.params.id, function(err, posts) { if (err) res.send(err); res.json(posts); }); }) .put(function(req, res) { Post.findById(req.params.id, function(err, posts) { if (err) res.send(err); posts.title = req.body.title; posts.description = req.body.description; posts.html = req.body.html; posts.photo = req.body.photo; posts.audio = req.body.audio; posts.video = req.body.video; posts.updateBy = req.body.updateBy; posts.updateDate = req.body.updateDate; posts.things = req.body.things; posts.save(function(err) { if (err) res.send(err); res.json({ message: 'Post updated!' }); }); }); }) .delete(function(req, res) { Post.remove({ _id: req.params.id }, function(err, posts) { if (err) res.send(err); res.json({ message: 'Successfully deleted' }); }); });router.route('/types') // create a types (accessed at POST http://localhost:2397/api/types) .post(function(req, res) { var types = new Type(); types.tag = req.body.tag; types.name = req.body.name; types.subType = req.body.subType; types.save(function(err) { if (err) res.send(err); res.json({ message: 'Type created!' }); }); }) // get all the types (accessed at GET http://localhost:2397/api/types) .get(function(req, res) { Type.find(function(err, types) { if (err) res.send(err); res.json(types); }); });// REGISTER OUR ROUTES -------------------------------// all of our routes will be prefixed with /apiapp.use('/api', router);// START THE SERVER// =============================================================================app.listen(port);console.log('Mongo Server running on port ' + port);
server.js

Ref:

Ref:

Ref:

Ref:

Ref:

(1)王路情,《Hadoop之MapReduce》:

(2)Suddenly,《Hadoop日记之MapReduce》:

(3)伯乐在线,《我是如何向老婆解释MapReduce的》:

(4)codingwu,《MapReduce原理与设计思想》:

(5)codingwu,《MapReduce实例浅析》:

(6)挑灯看剑,《图解MapReduce原理和执行过程》:

(7)万川梅、谢正兰,《Hadoop应用开发实战详解(修订版)》:

(8)张月,《Hadoop MapReduce开发最佳实践》:

转载于:https://www.cnblogs.com/ncore/p/5240650.html

你可能感兴趣的文章
获取鼠标的原始移动值
查看>>
Linux信号 编程
查看>>
有关滚动与位置
查看>>
Box2D自定义重力
查看>>
chpasswd
查看>>
mysqldump --single-transaction 和--lock-tables参数详解
查看>>
android 数据库_sql语句总结
查看>>
python购物车
查看>>
解决python2和python3的pip冲突
查看>>
面试/编程
查看>>
linux每日命令(16):head命令
查看>>
公司内部分享【富有成效的每日站会】总结
查看>>
打造一个上传图片到图床利器的插件(Mac版 开源)
查看>>
iOS横竖屏
查看>>
thinkphp判断更新是否成功
查看>>
Do While ... Loop 与 Do Until ... Loop 的区别
查看>>
【Linux】查询某个字符串出现次数
查看>>
高效使用jquery之一:请使用'On'函数
查看>>
冲刺第一周第三天
查看>>
ERP环境检测工具设计与实现 Environment Detection
查看>>