最新版express使用时的变化
原文:http://www.unfish.net/archives/772-20131207.html
npm install -g express-generator

express nodetest1
npm cache clean
npm install mongodb -g
npm start
app.use('/', routes);
app.use('/users', users);
并且这里还有另外一个问题:在老的版本中若是请求页面中的小模块,路由规则中直接用'.'的方式,如userlist.newuser,但是在新版本中不支持这种写法,只需写到文件名即可。
app.get('/userlist/newuser', userlist.newuser);//老版本
app.use('/userlist/newuser', userlist);//新的写法
6、读取mongodb中的数据并显示出来
//app.js
app.get('userlist',routes.userlist(db));
//userlist.js
exports.userlist=function(db){
return function(req, res){
var collection = db.get('usercollection');
collection.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
}
}
新版本下就需要改动如下:
//app.js
app.use('/userlist/newuser', userlist);
//userlist.js
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');
router.get('/', function(req,res) {
var collection = db.get('usercollection');
collection.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
});
module.exports = router;
7、向数据库写入数据
//app.js
app.post('/adduser', routes.adduser(db));
//userlist.js
exports.adduser = function(db) {
return function(req, res) {
// Get our form values. These rely on the "name" attributes
var userName = req.body.username;
var userEmail = req.body.useremail;
// Set our collection
var collection = db.get('usercollection');
// Submit to the DB
collection.insert({
"username" : userName,
"email" : userEmail
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("userlist");
// And forward to success page
res.redirect("userlist");
}
});
}
}
//app.js
app.use('/userlist/newuser', userlist);
//userlist.js
router.post('/newuser',function(req, res){
var userName = req.body.username;
var userEmail = req.body.useremail;
var collection = db.get('usercollection');
collection.insert({
"username" : userName,
"email" : userEmail
}, function (err, doc) {
if (err) {
res.send("很抱歉,添加数据失败!");
}
else {
//res.location("userlist");
res.redirect("back");
res.send("恭喜您,数据添加成功!");
console.log(doc);
}
});
});
module.exports = router;
最新版express使用时的变化的更多相关文章
- 新版MySQL开始使用时遇到的问题(时区、权限):
新版MySQL(本人Server version: 8.0.15)在刚开始使用时遇到的问题: 查看mysql安装版本:命令窗口 时区问题解决(The server time zone value 'Ö ...
- jqueryUI中datepicker的使用,解决与asp.net中的UpdatePanel联合使用时的失效问题
1.jqueryUI的datepicker的使用 -->首先在jqueryUI官网上根据你的需要下载适合你系统主题的样式,jqueryUI主题下载地址: -->下载后的文件 jquery- ...
- Saiku嵌入系统使用时传参数访问saiku(十六)
Saiku嵌入系统使用时传参数访问saiku Saiku通过iframe嵌入其他系统使用时,我们可以设定参数信息,然后根据url中参数对结果进行筛选哦. 这里我们实现的是根据日期字段进行范围查询,UR ...
- Android插件化(三):OpenAtlas的插件重建以及使用时安装
Android插件化(三):OpenAtlas的插件重建以及使用时安装 转 https://www.300168.com/yidong/show-2778.html 核心提示:在上一篇博客 An ...
- Cookie使用时需要注意个数及大小限制
各浏览器对Cookie有一定的限制,在使用时需要格外注意. 各浏览器之间对cookie的不同限制: IE6.0 IE7.0/8.0/9.0+ Opera FF Safari Chrome cook ...
- EntityFrameWork 使用时碰到的小问题
EntityFrameWork 使用时碰到的小问题 1,在使用orm访问数据库的相目里,也要引用EntityFrameWork.dll,否则无法使用orm 否则,编译错误 错误 5 "Sys ...
- MySQL 安装和启动服务,“本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止。”
MySQL 安装和启动服务,以及遇到的问题 MySQL版本: mysql-5.7.13-winx64.zip (免安装,解压放到程序文件夹即可,比如 C:\Program Files\mysql-5. ...
- MaterialCalendarView使用时遇到的问题
一.概述 MaterialCalendarView是一个开源项目.功能强大支持多选.单选.标注等. 二.问题 1.其继承自ViewGroup,故与CalendarView半毛钱关系都没有,完全是一个新 ...
- [备忘][转]rsync使用时的常见问题
sync使用时的常见问题: 错误1: rsync: read error: Connection reset by peer (104) rsync error: error in rsync pro ...
随机推荐
- PHP编译configure时常见错误
PHP的安装虽然有时候很简单,可是如果应用一多,我们安装起来就很头痛了!出错最多的就是安装PHP扩展的时候了.其实不管是你是Apache类的应用还是Nginx类的,PHP的安装都不是很简单,虽然网上有 ...
- mysql 数据库复制表 create table city1 like city;
-- 只复制表结构 create table city1 like city; INSERT INTO test2 SELECT * FROM test; -- 上面的表必须存在 -- 复制整张表的数 ...
- 个人博客开发之 ueditor 安装
- 【JavaEE】Springmvc+Spring+Hibernate整合及example
前面两篇文章,分别介绍了Springmvc和Spring的搭建方法,本文再搭建hibernate,并建立SSH最基本的代码结构. Hibernate和前面两个比就比较复杂了,Hibernate是一个o ...
- WPF TextBox 验证输入
//验证输入为数字private void txt_time_KeyDown(object sender, KeyEventArgs e){ if (!((e.Key >= Key.D0 &am ...
- Cut the rope
http://acm.nyist.net/JudgeOnline/problem.php?pid=651 描述We have a rope whose length is L. We will cut ...
- 水仙花数-python
题目: 求999以内的水仙花数? 分析: 如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数. 例如:1^3 + 5^3+ 3^3 = 153,因此153就是一个水仙花数 我们需计算出153的 ...
- 冒泡排序-python
题目: 如果一个list是一组打乱的数字 list1=[3,2,1,9,10,78,6] 如何用python将这组打乱的数字进行冒泡排序? 题解: def sort(nums): for i in r ...
- Laravel 5.1 数组帮助函数(随发现更新)
array_add 向一个数组中添加指定键值,如果键值不存在则添加,如果键本身就存在 那么就不添加: $test_array = ['name' => '大K', 'age' => 27] ...
- WPF简介:VS创建桌面应用程序
1.简介 1/ 什么是WPF WPF,Windows Presentation Foundation也,译过来就是"Windows呈现基础",你看它的目的非常明确,就是用来把数据& ...