6. delete.js:
var mongodb = require('mongodb')
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://localhost:27017/sun';
var URL = require('url');
var express = require('express');
var router = express.Router();
function deleteData(client, params, callback)
{
var db = client.db('sun');
var devices = db.collection('test');
var data = {"name":params.name};
devices.remove(data, function(error, result){
if (error) {
console.log('Error:'+ error);
}else{
console.log(result.result.n);
}
callback(result.result.n);
})
}
router.get('/delete', function(req, res, next){
var params = URL.parse(req.url, true).query;
MongoClient.connect(DB_CONN_STR, function(err, client) {
console.log("连接成功!");
deleteData(client, params, function(result) {
res.send("Delete Success:" + result);
client.close();
console.log("连接断开!");
});
});
})
module.exports = router;
7. sun.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('sun', { title: 'Sun'});
});
module.exports = router;
8. sun.ejs:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function mongoInsert() {
$.ajax({
url: "http://localhost:3000/mongo/insert",//接口服务器地址
dataType:"text",
type:"get",
data:{"name":document.getElementById("insertName").value,
"age":document.getElementById("insertAge").value,
"job":document.getElementById("insertJob").value},//请求数据
success:function(data){
alert("插入成功");
},
error:function(e){
alert("Error:" + e.status+','+ e.statusText);
}
})
}
function mongoUpdate() {
$.ajax({
url: "http://localhost:3000/mongo/update",//接口服务器地址
dataType:"text",
type:"get",
data:{"oldName":document.getElementById("oldName").value,
"newName":document.getElementById("newName").value},//请求数据
success:function(data){
alert("更新成功");
},
error:function(e){
alert("Error:" + e.status+','+ e.statusText);
}
})
}
function mongoDelete() {
$.ajax({
url: "http://localhost:3000/mongo/delete",//接口服务器地址
dataType:"text",
type:"get",
data:{"name":document.getElementById("deleteName").value},//请求数据
success:function(data){
alert("删除成功");
},
error:function(e){
alert("Error:" + e.status+','+ e.statusText);
}
})
}
function mongoFind() {
$.ajax({
url: "http://localhost:3000/mongo/find",//接口服务器地址
dataType:"text",
type:"get",
data:{"name":document.getElementById("findName").value},//请求数据
success:function(data){
var jsonData = $.parseJSON(data);
document.getElementById("divList").innerHTML = "<br>Result:<br>----------------<br>";
for (i=0;i<jsonData.length;i++){
document.getElementById("divList").innerHTML += "Name:" + jsonData[i].name
+ "<br>Age:" + jsonData[i].age
+ "<br>Job:" + jsonData[i].job
+ "<br>----------------<br>";
}
},
error:function(e){
document.getElementById("divList").innerHTML = "Error:" + e.status+','+ e.statusText;
}
})
}
</script>
</head>
<body>
<h1><%= title %>'s Test for nodeJS + MongoDB + Express + Async</h1>
<p>Welcome to <%= title %></p>
<ul>
<li style="margin-bottom:10px">
后台接口调用:<a href="http://localhost:3000/users/getUserInfo?id=1">getUserInfo</a>
</li>
<li style="margin-bottom:10px">
MongoDB新增数据:Name:<input id="insertName"> Age:<input id="insertAge"> Job:<input id="insertJob"> <button id="btnInsert" onclick="mongoInsert();">新增</button>
</li>
<li style="margin-bottom:10px">
MongoDB修改数据:NameOld:<input id="oldName"> NameNew:<input id="newName"> <button id="btnUpdate" onclick="mongoUpdate();">修改</button>
</li>
<li style="margin-bottom:10px">
MongoDB删除数据:Name:<input id="deleteName"> <button id="btnDelete" onclick="mongoDelete();">删除</button>
</li>
<li>
从MongoDB检索数据:Name:<input id="findName"> <button id="btnFind" onclick="mongoFind();">检索</button><a href="http://localhost:3000/mongo/find?name=sun">Find</a>
<div id="divList">
</div>
</li>
</ul>
</body>
</html>
9. 画面效果如下:
整个实例在这里下载:NodeJS+MongoDB实例
既然说到了引用Module,那就顺便把模块的添加也说一下,命令:
npm install --save -dev mongodb
“--save -dev”的用处是把引用模块的信息在安装成功后自动写入package.json,如果你想自己写,那么只要用
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-74442-9.html
其实今年以来国家实际上放了30000亿的水
我们家千玺配的棒棒的
包装前就有了呢