天堂国产午夜亚洲专区-少妇人妻综合久久蜜臀-国产成人户外露出视频在线-国产91传媒一区二区三区

node.js開發(fā)指南 mobi_使用Express3.0實(shí)現(xiàn)Node.js開發(fā)指南中的微博系統(tǒng)

發(fā)布時(shí)間:2016-09-18 14:17

  本文關(guān)鍵詞:Node.js開發(fā)指南,由筆耕文化傳播整理發(fā)布。


特別說(shuō)明:本實(shí)例僅在windows xp sp3系統(tǒng)下測(cè)試通過(guò)(其它系統(tǒng)未經(jīng)過(guò)測(cè)試)。

<Node.js開發(fā)指南>這本書,之前有評(píng)論過(guò),但之前并不清楚express2.x與3.x會(huì)有如此大的差異,導(dǎo)致在寫例子的過(guò)程中痛苦不已。為了避免更多的同學(xué)在學(xué)習(xí)書的例子時(shí),撞的頭破血流,覺得還是有必要分享一下自己這次痛苦的經(jīng)歷。

講實(shí)話,學(xué)習(xí)不是特別穩(wěn)定和成熟的技術(shù)風(fēng)險(xiǎn)不小,例如這個(gè)express。3.x就刪除了很多2.x的特性和功能(但好不容易買本書,書上并沒有地方特別指出版本差異的問(wèn)題,導(dǎo)致一開始就掉進(jìn)一個(gè)坑里去了)。先看看書中的2.x與目前最新版本的3.x它們之間的差異:

2.x到3.x的遷移(Migrating from 2.x to 3.x)

刪除:

1: res.render() "status" option (use node's res.statusCode= or res.status(code).render(...)) 2: res.render() "charset" option (use res.charset=) 3: res.local(name, value) (use res.locals.name = value or res.locals({ name: value }) instead) 4: app.dynamicHelpers() (use middleware + res.locals) 5: app.helpers() (use app.locals) 6: the concept of a "layout" (template engine specific now) 7: partial() (template engine specific) 8: res.partial() 9: "view options" setting, use app.locals 10: "hints" setting 11: req.isXMLHttpRequest (use req.xhr) 12: app.error() (use middleware with (err, req, res, next)) 13: req.flash() (just use sessions: req.session.messages = ['foo'] etc) 14: connect-flash can be used as middleware to provide req.flash()

發(fā)生發(fā)動(dòng)的有:

1: req.header(field[, defaultValue]) replaced by req.get(field) (remains for backwards compatibility) 2: res.header(field[, value]) replaced by res.set(field, value) / res.get(field) (remains for backwards compatibility) 3: res.send(body[, code]) is now res.send([code,] body) 4: res.redirect(url[, code]) is now res.redirect([code,] url) 5: res.json(obj[, code]) is now res.json([code,] obj) 6: renamed app.register() to app.engine() 7: template engine compliance from engine.compile(str, options) => Function to engine.__express(filename, options, callback) 8: express.createServer() is now simply express() (but remains for BC) 9: 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就得安裝“插件”來(lái)支持了。

 

廢話不多說(shuō)了,分享一下使用Express3.x來(lái)實(shí)現(xiàn)書中微博系統(tǒng)的例子。

1、使用express projectName創(chuàng)建項(xiàng)目時(shí),express的-t參數(shù)已經(jīng)失效,你得手修改package.json和app.js文件來(lái)指定模塊引擎,默認(rèn)的為jade;因?yàn)閖ade模塊寫起來(lái)實(shí)在是讓人蛋疼不已,我強(qiáng)烈建議換成ejs。這樣你需要修改的文件:

app.js

package.json (使用*默認(rèn)會(huì)獲取最新的)

2、connect-mongo的用法發(fā)生了變化,你需要使用下面的方法才行

1: var MongoStore = require('connect-mongo')(express);

 

3、3.x默認(rèn)已經(jīng)不支持flash了,你需要先使用npm install connect-flash。然后在app.js中添加如下代碼:

1: var flash = require('connect-flash'); 2:  3: app.configure(function(){ 4: app.use(flash()); 5: });

注意上述的代碼,app.use(flash());要放在session之前(這個(gè)是我試出來(lái)的,原因還沒去搞明白)

 

4、不支持ejs模塊的partials方法,你需要使用npm install express-partials,然后在app.js中添加如下代碼:

1: var partials = require('express-partials'); 2:  3: app.use(partials());

5、在使用res.render時(shí)需要顯式傳入模塊可能要用到的變量和數(shù)據(jù),在使用partial時(shí),,也需要指定。ex:

1: exports.index = function(req, res){ 2: Post.get(null, function(err, posts) { 3: if (err) { 4: posts = []; 5: } 6: res.render('index', { 7: title: '首頁(yè)', 8: posts : posts, 9: user : req.session.user, 10: success : req.flash('success').toString(), 11: error : req.flash('error').toString() 12: }); 13: }); 14: };

需要在render時(shí)傳入相應(yīng)的數(shù)據(jù){user:xx, error:xx}

index.ejs中如果需要載入其它ejs文件(例如同級(jí)目錄下的posts.ejs文件)

1: <%- partial('posts', {posts:posts}) %>

如果不傳入{posts:posts}的話,posts.ejs在使用posts會(huì)報(bào)錯(cuò)。

6、在使用mongodb來(lái)存儲(chǔ)sessions時(shí),你需要先安裝MongoDB。如果安裝MongoDB?,請(qǐng)參考這里>>

 

最后來(lái)張實(shí)際運(yùn)行的效果圖:

 

node.js開發(fā)指南 mobi_使用Express3.0實(shí)現(xiàn)Node.js開發(fā)指南中的微博系統(tǒng)

 

本示例用到的nodejs、MongoDB還有express等文件,已全部打包到一個(gè)文件中,有興趣的同學(xué)可以從這里進(jìn)行下載>>

 

如果對(duì)本實(shí)例有任何疑問(wèn)或者有興趣與我進(jìn)行交流、討論,可以使用E-mail與我聯(lián)系(email地址詳見blog左上角),不保證立即回復(fù),敬請(qǐng)諒解:)


  本文關(guān)鍵詞:Node.js開發(fā)指南,由筆耕文化傳播整理發(fā)布。



本文編號(hào):117429

資料下載
論文發(fā)表

本文鏈接:http://sikaile.net/wenshubaike/mishujinen/117429.html


Copyright(c)文論論文網(wǎng)All Rights Reserved | 網(wǎng)站地圖 |

版權(quán)申明:資料由用戶7bee7***提供,本站僅收錄摘要或目錄,作者需要?jiǎng)h除請(qǐng)E-mail郵箱bigeng88@qq.com