最新版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 ...
随机推荐
- C++数组类型与函数类型
之所以将C++的数组类型与函数类型拿到一块说,是因为两者在很多地方都一样. 首先,声明形式上类似: 数组类型: type [num] ...
- Spring MVC复选框
以下示例显示如何在使用Spring Web MVC框架的表单中使用复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...
- 在MathType中输入罗马数字的方法
MathType作为数学公式编辑器的编辑功能非常的强大,其中包含了许许多多各种各样的数学符号,甚至标记符号也很全面.编辑公式时有时为了让公式看起来会更有条理,会进行一定的序号设置,当然也可以对公式进行 ...
- go语言发送邮件
package main import ( "fmt" "net/smtp" "strings" ) //发送邮件的逻辑函数 func Se ...
- Genymotion Android模拟器与fiddler 数据包拦截
Genymotion: https://www.genymotion.com/fun-zone/ https://www.genymotion.com/account/create/ cls清空记录 ...
- Eclipse: Android Device Chooser - Unknown Target
公司最近所有的项目都使用到了Android开发手机(或PDA)应用.所需要的Android开发技术并不是非常复杂,因为我们的底层方法全部使用WebServcie写好了,做Android开发的人员只需要 ...
- Introduction to Mathematical Thinking - Week 6 - Proofs with Quantifieers
Mthod of proof by cases 证明完所有的条件分支,然后得出结论. 证明任意 使用任意 注意,对于一个任意的东西,你不知道它的具体信息.比如对于任意正数,你不知道它是 1 还是 2等 ...
- Docker + ElasticSearch + Node.js
最近有空就想研究下ElasticSearch. 此篇文章用来记录研究过程.备注:需要有一定的docker基础,ElasticSearch的基本概念 Docker安装ElasticSearch 首先,就 ...
- 浅析TCP/IP
TCP/IP概述 TCP/IP起源于1969年美国国防部(DOD:The United States Department Of Defense)高级研究项目管理局(APRA:AdvancedRese ...
- php 使用sendmail发送邮件
php 使用sendmail发送邮件 1.配置php.ini SMTP=smtp.163.com sendmail_from = 17760273453@163.com sendmail_path = ...