mongod.exe 数据库运行
mongo.exe 命令行对数据库的查阅
连接数据库是mongod.exe
mongo.exe是可以查看数据表的,所以必须先mongod.exe
启动起来才能启动mongo.exe
npm init 会生成package.json文件,这是一个包管理的文件
之后利用npm install 下载的文件都会纪录在json文件中
最好先初始化之后再安装文件
node_modules是使用npm install所安装的目录文件
mongodb的命令
model文件夹 model.Js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var demoSchema = new Schema({
uid : String,
title:String,
content:String,
createTime : { type: Date, default: Date.now }
});
exports.Demo = mongoose.model('Demo',demoSchema);
routes文件夹 index.js
var mongoose = require('mongoose');
var model = require('../model/demo.js');
var Demo = model.Demo;
mongoose.connect('mongodb://localhost/myWeb');
module.exports = function(app){
app.get('/', function(req, res){
Demo.find(function(err,docs){//没有查询条件,返回全部值
res.render('index', {
title:'Express Demo Example',
demos:docs
});
});
})
app.get('/add.html',function(req, res){
console.log('----here');
res.render('add.html', {title :'添加 demo list'});
});
app.post('/add.html', function(req, res){
var demo = new Demo({
uid : req.body.uid,
title: req.body.title,
content : req.body.content
});
console.log('create----');
demo.save(function(err,doc){
console.log(doc);
res.redirect('/');
});
});
app.get('/del.html',function(req, res){
var id = req.query.id;
console.log('id = ' + id);
if(id && '' != id) {
console.log('----delete id = ' + id);
Demo.findByIdAndRemove(id, function(err, docs) {
console.log('delete-----'+ docs);
res.redirect('/');
});
}
});
app.get('/modify.html', function(req, res){
var id = req.query.id;
console.log('id = ' + id);
if(id && '' != id) {
console.log('----delete id = ' + id);
Demo.findById(id, function(err, docs){
console.log('-------findById()------' + docs);
res.render('modify.html',{title:'修改ToDos',demo:docs});
});
};
});
app.post('/modify.html', function(req, res){
var demo = {
uid : req.body.uid,
title: req.body.title,
content : req.body.content
};
var id = req.body.id; //因为是post提交,所以不用query获取id
if(id && '' != id) {
console.log('----update id = ' + id + "," + demo);
Demo.findByIdAndUpdate(id, demo,function(err, docs) {
console.log('update-----'+ docs);
res.redirect('/');
});
}
});
app.post('/add.html', function(req, res){
var demo = new Demo({
uid : req.body.uid,
title: req.body.title,
content : req.body.content
});
console.log('create----');
demo.save(function(err,doc){
console.log(doc);
res.redirect('/');
});
});
}
views文件夹 index.html
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='../static/css/style.css' />
<meta charset="utf-8" />
<style type="text/css">
table { border:1px solid green;}
table thead tr th{ border:1px solid green;}
table tbody tr td{ border:1px solid green;}
</style>
</head>
<body>
<p>
<a href="add.html">增加</a>
</p>
<h1>DEMO List</h1>
<table>
<thead>
<tr>
<th>id</th>
<th>uid</th>
<th>title</th>
<th>content</th>
<th>createTime</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<% demos.forEach(function( demo ){ %>
<tr>
<td><%=demo._id%></td>
<td><%= demo.uid %></td>
<td><%= demo.title %></td>
<td><%= demo.content %></td>
<td><%= demo.createTime %></td>
<td><a href="del.html?id=<%=demo._id%>">Delete</a> | <a href="modify.html?id=<%=demo._id%>">Update</a></td>
</tr>
<% }); %>
</tbody>
</table>
</body>
</html>
add.html
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<meta charset="utf-8" />
<link rel='stylesheet' href='../static/css/style.css' />
<style type="text/css">
</style>
</head>
<body>
<h1>
增加
</h1>
<form method="post">
uid : <input type="text" name="uid"/><br/>
title:<input type="text" name="title"/><br/>
content:<textarea name="content"></textarea><br/>
<input type="submit" value="submit"/>
<input type="reset" value="reset"/>
</form>
</body>
</html>
modifty.html
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='../static/css/style.css' />
<meta charset="utf-8" />
<style type="text/css">
</style>
</head>
<body>
<h1>
修改
</h1>
<form method="post">
uid : <input type="text" name="uid" value="<%=demo.uid%>"/><br/>
title:<input type="text" name="title" value="<%=demo.title%>"/><br/>
content:<textarea name="content"><%=demo.content%></textarea><br/>
<input type="hidden" name="id" value="<%=demo._id%>"/>
<input type="submit" value="submit"/>
<input type="reset" value="reset"/>
</form>
</body>
</html>