MongoDB 安装步骤总结:

1、解压目录到d盘 mongodb

2、安装目录的下新建文件mongo.config文件

##store data here
dbpath=D:\mongodb\data
 
##all output go here
logpath=D:\mongodb\log\mongo.log
 
##log read and write operations
diaglog=3
3、d:\mongodb\bin>mongod --config D:\mongodb\mongo.config
4、d:\mongodb\bin> mongod --config D:\mongodb\mongo.config --install

net start MongoDB
net stop MongoDB
mongod --remove
1、创建node项目

express -e demo

2、初始化相关组件

npm install
// 初始化ejs
npm install ejs
// 初始化express组件
npm install express
// 初始化mongdb组合的组件 
npm install mongoose
npm install express-mongoose
// node 默认启动后修改文件,页面不会立即体现,通过该组件可以使修改的文件实时在页面体现出来
npm install supervisor

3、运行app.js,访问:http://localhost:3000,查看页面,如看到以下页面则说明页面访问成功:

 
4、此时项目默认使用*.ejs作为页面的模板,为了方便我们吧ejs的后缀修改html
a) 修改app.js中内容:

var ejs = require('ejs'); //增加

app.engine('html',ejs.__express); //增加

//将app.set('view engine', 'ejs'); 修改为app.set('view engine', 'html');

b) 重命名views下的index.ejs为index.html

5、重新启动服务访问,如果页面可以正常显示则说明修改成功。
 
6、现在开始写增删改查的详细方法
 
1)修改路由(app.js)

app.get('/add.html',routes.add); //跳转到添加页面
app.post('/add.html', routes.create);//添加记录
app.get('/del.html',routes.delById);//删除
app.get('/modify.html', routes.toModify); //跳转到修改页面
app.post('/modify.html', routes.modify);//修改数据

2)在routes目录下创建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); //此处Demo会自动对应数据库中的demos表,不知道是不是可以修改,反正我是这个认为的。

3)修改index.js,添加逻辑方法

/*
* GET home page.
*/
var mongoose = require('mongoose');
var model = require('./model');
var Demo = model.Demo;

mongoose.connect('mongodb://localhost/monkey');

exports.index = function(req, res){
//查询所有数据,保存到demos中,在页面循环输出
Demo.find(function(err,docs){
res.render('index', {
title:'Express Demo Example',
demos:docs
});
});

};

//跳转到添加页面
exports.add = function(req, res) {
console.log('----here');
res.render('add.html', {title :'添加 demo list'});
};

//创建新纪录
exports.create = 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('/');
});

};

// 根据id删除相应的记录
exports.delById = 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('/');
});
}

};

// 查询对应修改记录,并跳转到修改页面
exports.toModify = 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});
});
};
};

//修改相应的值
exports.modify = 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('/');
});
}

};

4)index.html页面

<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<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="http://houzhiqingjava.blog.163.com/blog/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="http://houzhiqingjava.blog.163.com/blog/del.html?id=<%=demo._id%>">Delete</a> | <a href="http://houzhiqingjava.blog.163.com/blog/modify.html?id=<%=demo._id%>">Update</a></td>
</tr>
<% }); %>
</tbody>
</table>
</body>
</html>

5)add.html页面

<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/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>

6) 修改页面modify.html

<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</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>

node.js + express(ejs) + mongodb(mongoose) 增删改实例的更多相关文章

  1. React+Node.js+Express+mongoskin+MongoDB

    首发:个人博客,更新&纠错&回复 采用React + Node.js + Express + mongoskin + MongoDB技术开发的一个示例,演示地址在这里,项目源码在这里. ...

  2. node.js(express)连接mongoDB入门指导

    一.写在前面 人人都想成为全栈码农,作为一个web前端开发人员,通往全栈的简洁之路,貌似就是node.js了.前段时间学习了node.js,来谈谈新手如何快速的搭建自己的web服务,开启全栈之路. 二 ...

  3. [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站

    原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...

  4. [MEAN Stack] First API -- 1. with Node.js, Express and MongoDB

    Learn how to import data into your MongoDB and then use Express to serve a simple Node.js API. Impor ...

  5. [转] Creating a Simple RESTful Web App with Node.js, Express, and MongoDB

    You can find/fork the sample project on GitHub Hey! This and all my other tutorials will soon be mov ...

  6. Node.js + MySQL 实现数据的增删改查

    通过完成一个 todo 应用展示 Node.js + MySQL 增删改查的功能.这里后台使用 Koa 及其相应的一些中间件作为 server 提供服务. 初始化项目 $ mkdir node-cru ...

  7. node.js中对 mysql 进行增删改查等操作和async,await处理

    要对mysql进行操作,我们需要安装一个mysql的库. 一.安装mysql库 npm install mysql --save 二.对mysql进行简单查询操作 const mysql = requ ...

  8. express+mongodb+mongoose增删改查

    增加 修改 删除 数据库 这是一个前后端分离的项目前端项目地址:https://gitee.com/dingshao/express_qd.git后端项目地址:https://gitee.com/di ...

  9. node.js—express+ejs、express+swig、

    安装:npm install -g express-generator 普通express 网站 创建:express testWeb 安装依赖:npm install 修改app.js文件并运行 找 ...

随机推荐

  1. html 第一阶段 学习使用总结

    基本使用内容: <html> <head> <title>Title of the document</title> <link rel=&quo ...

  2. 使用多线程完成Socket

    public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSocket=null ...

  3. mongodb的地理空间索引常见的问题

    创建地理空间索引注意事项 创建地理空间索引失败,提示错误信息如下 > db.places.ensureIndex({"loc":"2dsphere"}){ ...

  4. Lesson 5: Typography in Product Design

    Lesson 5: Typography in Product Design Article 1: Interactive Guide to Blog Typography 布局(Layout) 用空 ...

  5. C++ 文本读写

    写文件: ofstream of; of.open("test.txt"); string content = "abcd"; of.write(content ...

  6. 搭建ngrok服务器(ubuntu 14)-- 微信 80端口和IPC备案限制解决方案

    概述: ngrok其实这东西,我也不是很懂,所以也直接跟大家说,这就是个类似花生壳的东西. 简单来说,它就好像把我们内网自己使用的电脑和服务器用vpn连接起来,然后你的电脑就可以从互联网来访问了,有个 ...

  7. 网页通用的测试用例(出处:: 51Testing-- lxp1119216)

    此题的考察目的:面试者是否熟悉各种测试方法,是否有丰富的Web测试经验, 是否了解Web开发,以及设计Test case的能力 这个题目还是相当有难度的, 一般的人很难把这个题目回答好. 首先,你要了 ...

  8. vs code(egret wing) php配置与调试

    所需插件 下面是便于编写以及调试php的插件,可以从IDE Store中搜索. PHP Debug,PHP IntelliSense,PHP IntelliSence Cranne. 环境配置 找到项 ...

  9. Jquery插件模版

    ;(function($){ $.fn.jcDate = function(options) { var defaults = { IcoClass : "jcDateIco", ...

  10. js识别终端类型

    <script type="text/javascript"> function browserRedirect() { var sUserAgent= navigat ...