您的位置:首页 > Web前端 > Node.js

[转载]使用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?,请参考这里>>

最后来张实际运行的效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐