Nodejs+Mongo+WebAPI
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Nodejs+Mongo+WebAPI,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3655字,纯文字阅读大概需要6分钟。
内容图文
Nodejs+Mongo+WebAPI集成
1.【 目录】:
|- models/bear.js
|- node_modules/
|- express
|- mongoose
|- body-parser
|- Server.js
|- package.json
2. 【代码】:
//Server.js
1 // server.js 2 3 // base setup 4 // =========================================================== 5 6 // call the package we need 7 var express = require(‘express‘); // call expreess 8var app = express(); // define our app using express 9var bodyParser = require(‘body-parser‘); 10 11// mongoose setup 12var mongoose = require(‘mongoose‘); 13 mongoose.connect(‘mongodb://localhost:27017/myDatabase‘) // connect to database ‘myDatabase‘ 14 15// models setup 16var Bear = require(‘./models/bear‘) 17 18// configure app to use bodyParser() 19// this will let us get the data from a POST 20 app.use( bodyParser.urlencoded({ extended: true})); 21app.use( bodyParser.json()); 22 23var port = process.env.PORT || 3000; 24 25 26 27// routes for our API 28// ============================================================== 29var router = express.Router(); 30 31 32// 1. middle ware 33router.use( function(req, res, next){ 34// do logging 35 console.log(‘Something is happening.‘); 36 next(); 37}); 38 39// 2. test route to make sure everything is work 40 router.get(‘/‘, function(req, res){ 41 res.json( { message: ‘Horray! Welcome to our api!‘}); 42}); 43 44// 3. routes end in: /bears 45 router.route(‘/bears‘) 46 47// 3-1. create a bear (accessed at POST http://localhost:3000/api/bears) 48 .post( function(req, res){ 49var newBear = new Bear(); // create a new instance of the Bear model 50 newBear.name = req.body.name; // set the bears name ( comes from the request ) 51 52// save the bear and check for errors 53 newBear.save ( function(err){ 54if(err) 55 res.send(err); 56 res.json({ message: ‘Bear created!‘}); 57 }); 58 }) 59 60// 3-2. get all the bears (accessed at GET http://localhost:3000/api/bears) 61 .get( function(req, res){ 62 Bear.find( function(err, bears){ 63if(err) 64 res.send(err); 65 res.json(bears); 66 }) 67 } ); 68 69// 4. routes end in: /bears/:bears_id 70 router.route(‘/bears/:bear_id‘) 71 72//4-1. get the bear with this id (accessed at GET http://localhost:3000/api/bears/:bear_id) 73 .get(function(req, res){ 74 Bear.findById(req.params.bear_id, function(err, bear){ 75if(err) 76 res.send(err); 77 res.json(bear); 78 }); 79 }) 80 81// 4-2. update the bear with this id (accessed at PUT http://localhost:3000/api/bears/:bear_id) 82 .put(function(req, res){ 83 84// use bear model to find the bear we want 85 Bear.findById(req.params.bear_id, function(err, bear){ 86if(err) 87 res.send(err); 88 bear.name = req.body.name; // update the bears info 89 90// save the bear 91 bear.save(function(err){ 92if(err) 93 res.send(err); 94 res.json({message: ‘Bear updated!‘}); 95 }); 96 }); 97 }) 98 99// 4-3. delete the bear with this id (accessed at DELETE http://localhost:3000/api/bears/:bear_id)100 .delete(function(req, res){ 101 Bear.remove( 102 { 103 _id: req.params.bear_id 104 }, function(err, bear){ 105if(err) 106 res.send(err); 107 res.json({ message: ‘Successfully deleted!‘}); 108 } 109 ) 110 }); 111112// more routes for our API will happen here 113114// REGISTER OUR ROUTES 115116// all of our routes will be prefixed with /api117 app.use(‘/api‘, router); 118119120// start the server 121// ==============================================122app.listen(port); 123 console.log(‘Magic happens on port : ‘ + port);
// models/bear.js
1 var mongoose = require(‘mongoose‘); 2var Schema = mongoose.Schema; 34var BearSchema = new Schema( { 5 name: String 6}); 78 module.exports = mongoose.model(‘Bear‘, BearSchema);
原文:http://www.cnblogs.com/dzone/p/4979481.html
内容总结
以上是互联网集市为您收集整理的Nodejs+Mongo+WebAPI全部内容,希望文章能够帮你解决Nodejs+Mongo+WebAPI所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。