当前位置:Gxlcms > 数据库问题 > 关于nodejs+mongoDB的使用

关于nodejs+mongoDB的使用

时间:2021-07-01 10:21:17 帮助过:7人阅读

//express_demo.js 文件 2 var express = require(‘express‘); 3 var bodyParser = require(‘body-parser‘); 4 var app = express(); 5 6 app.all(‘*‘, function(req, res, next) { 7 // TODO 支持跨域访问 8 res.setHeader("Access-Control-Allow-Origin", "*"); 9 res.setHeader("Access-Control-Allow-Credentials", "true"); 10 res.setHeader("Access-Control-Allow-Methods", "*"); 11 res.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token"); 12 res.setHeader("Access-Control-Expose-Headers", "*"); 13 next(); 14 }); 15 16 //用bodyParser插件来获取post请求的参数 17 app.use(bodyParser.json()); 18 app.use(bodyParser.urlencoded({ extended: false })); 19 20 var mongoose = require("mongoose"); 21 mongoose.Promise = global.Promise; 22 // mongoose.set("debug",true); 23 mongoose.connect(‘mongodb://localhost:27017/myDB‘, { useMongoClient: true }); 24 var db = mongoose.connection; 25 db.on("error", function(error) { 26 console.log("数据库连接失败:" + error); 27 }); 28 29 db.on("open", function() { 30 console.log("数据库连接成功"); 31 }); 32 33 //分开的一种写法,实例用的是简写 34 // var Schema = mongoose.Schema; 35 // /*定义模式Student_Schema*/ 36 // var Student_Schema = new Schema({ 37 // name: String, 38 // id: Number, 39 // phone: String, 40 // date: Date 41 // }, { 42 // versionKey: false 43 // }); 44 // /*定义模型Student,注意数据库存的是students*/ 45 // mongoose.model("Student", Student_Schema); 46 var MyPerson = mongoose.model(‘persons‘, { 47 name: String, 48 phone: Number 49 }); 50 51 app.post(‘/add-person‘, function(req, res) { 52 var params = req.body; 53 if (!params.name || !params.phone) { 54 res.send({ ok: 1, msg: ‘参数错误‘ }); 55 return false; 56 } 57 var person = new MyPerson({ 58 name: params.name, 59 phone: params.phone 60 }); 61 person.save(function(err) { 62 if (err) { 63 console.log(err); 64 } else { 65 res.send({ ok: 0, msg: ‘新增成功‘ }); 66 } 67 }); 68 }) 69 70 app.get(‘/concat‘, function(req, res) { 71 res.send({ 72 ok: 0, 73 msg: ‘获取成功‘, 74 data: { 75 total: 1, 76 rows: [{ 77 name: "aa", 78 email: "bb", 79 company: "cc", 80 city: "dd" 81 }] 82 } 83 }) 84 }) 85 86 app.get(‘/list‘, function(req, res) { 87 // console.log(req); 88 // res.setHeader("Access-Control-Allow-Origin", "*"); 89 var params = req.query; 90 var conditions = {}; 91 var regExp = new RegExp(params.name, "gi"); 92 if (params.name) { 93 conditions = { ‘name‘: regExp }; 94 } 95 MyPerson.count(conditions, function(err, num) { 96 MyPerson.find(conditions, null, { skip: parseInt((params.page - 1) * params.per_page), limit: parseInt(params.per_page), sort: { "name": 1 } }, function(err, docs) { 97 if (err) { 98 console.error(err); 99 } else { 100 res.send({ 101 ok: 0, 102 msg: ‘获取成功‘, 103 data: { 104 total: num, 105 rows: docs 106 } 107 }) 108 } 109 }); 110 }); 111 }) 112 113 app.post(‘/update-person‘, function(req, res) { 114 var params = req.body; 115 if (!params.name || !params.phone) { 116 res.send({ ok: 1, msg: ‘参数错误‘ }); 117 return false; 118 } 119 var conditions = { _id: params._id }; 120 var updates = { $set: { name: params.name, phone: params.phone } }; 121 MyPerson.update(conditions, updates, function(error) { 122 if (error) { 123 console.error(error); 124 } else { 125 res.send({ ok: 0, msg: ‘修改成功‘ }); 126 } 127 }); 128 }) 129 130 app.post(‘/del-person‘, function(req, res) { 131 var params = req.body; 132 // console.log(params); 133 MyPerson.remove({ _id: params._id }, function(err) { 134 if (err) { 135 console.error(err); 136 } else { 137 res.send({ ok: 0, msg: ‘删除成功‘ }); 138 } 139 }); 140 }) 141 142 var server = app.listen(8081, function() { 143 144 var host = server.address().address 145 var port = server.address().port 146 console.log("应用实例,访问地址为 http://%s:%s", host, port) 147 148 })

注意点:

技术分享图片是对跨域的处理。

技术分享图片用post请求的时候,参数的获取需要加技术分享图片插件,然后通过技术分享图片拿到参数。

 

技术分享图片

skip是(过滤掉前面几个)即从哪个位置开始,limit是限制几个, sort是排序。

 

关于nodejs+mongoDB的使用

标签:连接   student   pre   model   post请求   res   ons   mod   ber   

人气教程排行