[转载]使用Express3.0实现<Node.js开发指南>中的微博系统
2012-11-01 18:07
495 查看
使用Express3.0实现<Node.js开发指南>中的微博系统
特别说明:本实例仅在windows xp sp3系统下测试通过(其它系统未经过测试)。
<node.js开发指南>这本书,之前有评论过,但之前并不清楚express2.x与3.x会有如此大的差异,导致在写例子的过程中痛苦不已。为了避免更多的同学在学习书的例子时,撞的头破血流,觉得还是有必要分享一下自己这次痛苦的经历。讲实话,学习不是特别稳定和成熟的技术风险不小,例如这个express。3.x就删除了很多2.x的特性和功能(但好不容易买本书,书上并没有地方特别指出版本差异的问题,导致一开始就掉进一个坑里去了)。先看看书中的2.x与目前最新版本的3.x它们之间的差异:
2.x到3.x的迁移(Migrating from 2.x to 3.x)
删除:
res.render() "status" option (use node's res.statusCode= or res.status(code).render(...))
res.render() "charset" option (use res.charset=)
res.local(name, value) (use res.locals.name = value or res.locals({ name: value }) instead)
app.dynamicHelpers() (use middleware + res.locals)
app.helpers() (use app.locals)
the concept of a "layout" (template engine specific now)
partial() (template engine specific)
res.partial()
"view options" setting, use app.locals
"hints" setting
req.isXMLHttpRequest (use req.xhr)
app.error() (use middleware with (err, req, res, next))
req.flash() (just use sessions: req.session.messages = ['foo'] etc)
connect-flash can be used as middleware to provide req.flash()
发生发动的有:
req.header(field[, defaultValue]) replaced by req.get(field) (remains for backwards compatibility)
res.header(field[, value]) replaced by res.set(field, value) / res.get(field) (remains for backwards compatibility)
res.send(body[, code]) is now res.send([code,] body)
res.redirect(url[, code]) is now res.redirect([code,] url)
res.json(obj[, code]) is now res.json([code,] obj)
renamed app.register() to app.engine()
template engine compliance from engine.compile(str, options) => Function to engine.__express(filename, options, callback)
express.createServer() is now simply express() (but remains for BC)
Use express.cookieParser('secret') instead of passing the secret to the express.session() middleware. The 'secret' option in the express.session() middleware has been removed.
以前可以直接用的很多特性,如果使用Express 3.x就得安装“插件”来支持了。
废话不多说了,分享一下使用Express3.x来实现书中微博系统的例子。
1、使用express projectName创建项目时,express的-t参数已经失效,你得手修改package.json和app.js文件来指定模块引擎,默认的为jade;因为jade模块写起来实在是让人蛋疼不已,我强烈建议换成ejs。这样你需要修改的文件:
app.js
package.json (使用*默认会获取最新的)
2、connect-mongo的用法发生了变化,你需要使用下面的方法才行
var MongoStore = require('connect-mongo')(express);
3、3.x默认已经不支持flash了,你需要先使用npm install connect-flash。然后在app.js中添加如下代码:
var flash = require('connect-flash');
app.configure(function(){
app.use(flash());
});注意上述的代码,app.use(flash());要放在session之前(这个是我试出来的,原因还没去搞明白)
4、不支持ejs模块的partials方法,你需要使用npm install express-partials,然后在app.js中添加如下代码:
var partials = require('express-partials');
app.use(partials());5、在使用res.render时需要显式传入模块可能要用到的变量和数据,在使用partial时,也需要指定。ex:
exports.index = function(req, res){
Post.get(null, function(err, posts) {
if (err) {
posts = [];
}
res.render('index', {
title: '首页',
posts : posts,
user : req.session.user,
success : req.flash('success').toString(),
error : req.flash('error').toString()
});
});
};
需要在render时传入相应的数据{user:xx, error:xx}
index.ejs中如果需要载入其它ejs文件(例如同级目录下的posts.ejs文件)
<%- partial('posts', {posts:posts}) %>如果不传入{posts:posts}的话,posts.ejs在使用posts会报错。
6、在使用mongodb来存储sessions时,你需要先安装MongoDB。如果安装MongoDB?,请参考这里>>
最后来张实际运行的效果图:
相关文章推荐
- 使用Express3.0实现<Node.js开发指南>中的微博系统
- 写一个MyList<T>的类,内部用T[]实现(不能使用系统的List<T>类),需要实现的接口如下:
- Cocos2d-x android使用onKeyDown监听返回键实现二次返回退出<转载>
- node.js实现微博系统在express3.0+版本下的实现——所遇问题
- 《node.js开发指南》微博系统 4.x版 2016年最新实现方法(基于mysql,使用sequelize)
- Spring Cloud版——电影售票系统<八>使用 Spring Sleuth 实现微服务追踪
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)
- Spring Cloud版——电影售票系统<三>使用Feign实现声明式REST调用
- <转载>Android中如何使用命令行查看内嵌数据库SQLite3
- 转载:android利用<shape>标签实现圆角矩形的功能
- 关于delegate的简单使用说明<转载两篇>
- <C语言>使用一个二维数组实现学生姓名管理系统,要求不能使用链表
- 转载只为记录经典之<使用linux中的sed编辑器>
- 只显示 前100个字 java 实现截取字符串!使用! <c:if test="${fn:length(onebeans.info)>100 }">${ fn:substri
- 使用JDK提供的ThreadLocal实现线程范围的共享变量(类似于Map<Thread,Variable>)
- PrimeFaces 4.0 使用 <p:fileDownload> 标签实现文件下载
- 使用Enumerable.OfType<T>扩展方法实现非泛型集合的Linq查询
- 艾伟_转载:数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- 想实现自己的地理应用程序?建议参考<<使用Rails和Ajax开发Google地图应用程序>>
- 使用ASP.NET Global.asax 文件<转载>