《Node.js開發指南》MicroBlog項目的問題匯總

來源:互聯網
上載者:User

最近對Node產生了點興趣,就看了《Node.js開發指南》一書,按照書中的例子敲完了所有代碼。書是好書,非常適合新手學習,但是例子中的代碼太舊,造成一些麻煩,以下列出來供大家參考:《Node.js開發指南》MicroBlog項目完整代碼下載


問題一:安裝 ejs 模板的文法有問題,安裝不成功,如下:express -t ejs microblog
需要改成:express microblog -e
問題二:partial 方法已經不能用了,可以用include代替,如下:<ul><%- partial('listitem', items) %></ul>
需要改成:<% items.forEach(function(listitem){ %><% include listitem %><% }) %>
問題三:helpers 和 dynamicHelpers 方法已經不能用了,如下:app.helpers({     inspect: function(obj) {          return util.inspect(obj, true);     }});app.dynamicHelpers({     headers: function(req, res) {          return req.headers;     }});app.get('/helper', function(req, res) {     res.render('helper', {          title: 'Helpers'     });});
需要改成:var util = require('util');app.locals({     inspect: function(obj){          return util.inspect(obj, true);     }});app.use(function(req, res, next){     res.locals.headers = req.headers;     next();});app.get('/helper', function(req, res){     res.render('helper',{          title: 'Helpers'     });});
還需要注意的是,上面這段代碼需要放在 app.use(app.router); 前面。
問題四:express3.*已經不支援layout方法了,所以要改成include才能正常顯示首頁。在 views 檔案夾下建立,header.ejs 和 footer.ejs。layout.ejs 中的內容,以 <%- body %> 為界限,上面的內容寫入header.ejs ,下面的內容寫入footer.ejs然後在 index.js 中加入 <% include header.ejs %> 和 <% include footer.ejs %>,把表單內容,寫在中間即可
問題五:配置mongodb時,很多報錯:app.js中的 var settings = require('../settings'); 應改成 var settings = require('./settings');app.js中的 app.use(express.bodyParser()); 應該去掉app.js中的 var MongoStore = require('connect-mongo'); 應改成 var MongoStore = require('connect-mongo')(express);
問題六:出現 has no method 'router' 問題,解決辦法如下:保留原來的 app.use(app.router); 不要按作者的說法改成 app.use(express.router(routes));並且在 app.js 最末尾加上 routes(app);而且還要刪除掉 app.js 中的app.get('/', routes.index);app.get('/u/:user', routes.user);app.post('/post', routes.post);app.get('/reg', routes.reg);app.post('/reg', routes.doReg);app.get('login', routes.login);app.post('login', routes.doLogin);app.get('/logout', routes.logout);
問題七:req.flash 方法不能用,解決辦法如下:運行>npm install connect-flash 安裝組件並在app.js中加入:var flash = require('connect-flash');app.use(flash());
問題八:Error: Cannot use a writeConcern without a provided callback    at Db.ensureIndex (D:\Work\code\nodejs\microblog\node_modules\mongodb\lib\mongodb\db.js:1395:11)解決辦法如下:\models\user.js 中的 collection.ensureIndex('name', {unique: true}); 改成collection.ensureIndex('name', {unique: true}, function(err, user){});\models\post.js 中的 collection.ensureIndex('user'); 改成collection.ensureIndex('user' ,function(err, post){});
其中參考了不少其他朋友的文章,也供大家參考:跟著《Node.js 開發指南》寫MicroBlog執行個體總結
使用Express3.0實現<Node.js開發指南>中的微博系統 
《node.js開發指南》(中文)觀後感      
express:node throwing error on mongodb 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.