代码如下
/** 1.npm install mongodb --save-dev / cnpm install mongodb --save-dev 2.var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/test'; 连接数据库的地址 3.连接数据库 MongoClient.connect(url, function(err, db) { }); 4.实现增加修改删除 MongoClient.connect(url, function(err, db) { db.collection('user').insertOne({'name':'zhangsan'},function(error,data){ }) }); */ var http=require('http'); var ejs=require('ejs'); var MongoClient = require('mongodb').MongoClient; /*引入数据库 MongoClient*/ var DBurl = 'mongodb://localhost:27017/test'; // 连接数据库的地址 student表示数据库的名称 var url=require('url'); /*引入url模块*/ var app=require('./model/express-route.js'); http.createServer(app).listen(3000); app.get('/',function(req,res){ var msg='这是数据库的数据' ejs.renderFile('views/index.ejs',{msg:msg},function(err,data){ res.send(data); }) }) app.get('/add',function(req,res){ //增加数据 MongoClient.connect(DBurl,function(err,db){ /*连接数据库*/ if(err){ console.log(err); console.log('数据库连接失败'); return; } //增加数据 db.collection('test').insertOne({ "name":"李四", "age":10 },function(error,result){ if(error){ console.log('增加数据失败'); return; } res.send('增加数据成功'); db.close();/*关闭数据库*/ }) }) }) app.get('/edit',function(req,res){ //增加数据 //res.send('修改数据成功'); MongoClient.connect(DBurl,function(err,db){ if(err){ console.log(err); console.log('数据库连接失败'); return; } db.collection('test').updateOne({"name":'lisi'},{$set:{ "age":666 }},function(error,data){ if(error){ console.log('修改数据失败'); return; } console.log(data); res.send('修改数据成功'); db.close();/*关闭数据库*/ }) }) }) app.get('/delete',function(req,res){ var query=url.parse(req.url,true).query; var name=query.name; MongoClient.connect(DBurl,function(err,db){ if(err){ console.log(err); console.log('数据库连接失败'); return; } db.collection('test').deleteOne({"name":"lisi"},function(error,data){ if(error){ console.log('删除失败'); return; } console.log(data); res.send('删除数据成功'); db.close(); }) }) }) app.get('/test',function (req,res) { console.log('这个是test'); res.send('这个是end') })
附上app
var url=require('url'); //封装方法改变res 绑定res.send() function changeRes(res){ res.send=function(data){ res.writeHead(200,{"Content-Type":"text/html;charset='utf-8'"}); res.end(data); } } //暴露的模块 var Server=function(){ var G=this; /*全局变量*/ //处理get和post请求 this._get={}; this._post={}; var app=function(req,res){ changeRes(res); //获取路由 var pathname=url.parse(req.url).pathname; if(!pathname.endsWith('/')){ pathname=pathname+'/'; } //获取请求的方式 get post var method=req.method.toLowerCase(); if(G['_'+method][pathname]){ if(method=='post'){ /*执行post请求*/ var postStr=''; req.on('data',function(chunk){ postStr+=chunk; }) req.on('end',function(err,chunk) { req.body=postStr; /*表示拿到post的值*/ //G._post['dologin'](req,res) G['_'+method][pathname](req,res); /*执行方法*/ }) }else{ /*执行get请求*/ G['_'+method][pathname](req,res); /*执行方法*/ } }else{ res.end('no router'); } } app.get=function(string,callback){ if(!string.endsWith('/')){ string=string+'/'; } if(!string.startsWith('/')){ string='/'+string; } // /login/ G._get[string]=callback; } app.post=function(string,callback){ if(!string.endsWith('/')){ string=string+'/'; } if(!string.startsWith('/')){ string='/'+string; } // /login/ G._post[string]=callback; //G._post['dologin']=function(req,res){ // //} } return app; } module.exports=Server();