Created
January 26, 2018 12:56
-
-
Save xuyanbo03/227a6afe1347a4d9bf34ff2b9e4765cb to your computer and use it in GitHub Desktop.
express and mongoose template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express=require("express") | |
const mongoose=require("mongoose") | |
//连接MongoDB数据库集合 | |
const DB_URL='mongodb://localhost:27017/app' | |
mongoose.connect(DB_URL) | |
mongoose.connection.on('connected',function(){ | |
console.log('mongo connect success') | |
}) | |
//建文档、字段 | |
const User=mongoose.model('user',new mongoose.Schema({ | |
user:{ | |
type:String, | |
require:true | |
}, | |
age:{ | |
type:Number, | |
require:true | |
} | |
})) | |
//新增数据 | |
// User.create({ | |
// user:'app', | |
// age:18 | |
// },function(err,doc){ | |
// if(!err){ | |
// console.log(doc) | |
// }else{ | |
// console.log(err) | |
// } | |
// }) | |
//删除数据 | |
// User.remove({user:'app'},function(err,doc){ | |
// if(!err){ | |
// console.log(doc) | |
// }else{ | |
// console.log(err) | |
// } | |
// }) | |
//更新数据 | |
// User.update({user:'app'},{'$set':{age:20}},function(err,doc){ | |
// if(!err){ | |
// console.log(doc) | |
// }else{ | |
// console.log(err) | |
// } | |
// }) | |
//新建app | |
const app=express(); | |
const PORT=6666 | |
app.get('/',function(req,res){ | |
res.send('<h1>Hello World</h1>') | |
}) | |
app.get('/data',function(req,res){ | |
User.findOne({user:'app'},function(err,doc){ | |
res.json(doc) | |
}) | |
//res.json({name:'app',type:'IT'}) | |
}) | |
app.listen(PORT,function(){ | |
console.log('Node app start at port '+PORT) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment