Recently, I became interested in Node. js. I read the book Node. js Development Guide and completed all the code according to the examples in the book. Books are good books and are very suitable for beginners to learn, but the code in the example is too old, causing some trouble. The following is a list for your reference: full code download from the MicroBlog project in Node. js Development Guide
Question 1:An error occurred while installing the syntax of the ejs template. The installation fails as follows: express-t ejs microblog
Change to: express microblog-e
Question 2:The partial method is no longer available and can be replaced by include, as shown below: <ul> <%-partial ('listitem', items) %> </ul>
Change to <% items. forEach (function (listitem) {%> <% include listitem %> <%}) %>
Question 3:The helpers and dynamicHelpers methods are no longer available, as shown below: 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 '});});
You need to change it to 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 '});});
Note that the above Code should be placed in front of app. use (app. router.
Question 4:Express3. * The layout method is not supported, so you must change it to include to display the homepage normally. Create a new header. ejs and footer. ejs in the views folder. Layout. the content in ejs is bounded by <%-body %>. The content above is written into the header. in ejs, the following content is written to footer. then, in index. add <% include header. ejs %> and <% include footer. ejs %>, write the Form Content in the middle.
Question 5:Many errors are reported during mongodb configuration: app. var settings = require ('.. /settings'); var settings = require ('. /settings'); app. app in js. use (express. bodyParser (); remove the app. var MongoStore = require ('connect-mongo ') in js; change var MongoStore = require ('connect-mongo') (express );
Question 6:The has no method 'router 'problem occurs. The solution is as follows: keep the original app. use (app. router); Do not change it to the app according to the author's statement. use (express. router (routes); and in the app. add routes (app) at the end of js, and delete the app. app in js. 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 );
Question 7:Req. the flash method cannot be used. The solution is as follows: Run> npm install connect-flash to install the component and install it in the app. add var flash = require ('connect-flash'); app in js. use (flash ());
Question 8: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) solution: \ models \ user. collection in js. ensureIndex ('name', {unique: true}); change to collection. ensureIndex ('name', {unique: true}, function (err, user) {}); \ models \ post. collection in js. ensureIndex ('user'); changed to collection. ensureIndex ('user', function (err, post ){});
Among them, I have referenced many other friends' posts for your reference: Follow the Node. js Development Guide to write a summary of the MicroBlog instance.
Use Express3.0 to implement the Weibo System in <Node. js Development Guide>
View the background of node. js Development Guide (Chinese)
Express: node throwing error on mongodb