has been dedicated to front-end development, in recent days, began to learn the Nodejs. As a front-end developer, it is very exciting to see such a background written in JavaScript. However, backstage is different from the front end, in the process of learning, or will encounter a lot of problems.
In order to start learning Nodejs, a first choice of "Simple Nodejs" this book, read a few chapters, come to a conclusion is: Really a good book, but still can not write Nodejs. Then chose another textbook "Nodejs Development Guide", because read the "simple Nodejs", directly skipped the book in the first few chapters, wrote the book fifth chapter of the microblog example. As a novice, in the process of writing, only gradually found that because the express version of the reason for the upgrade, the book is a lot of code can not be used, which for beginners, it is really painful experience. In the spirit of sharing and learning, I hereby offer the NODEJS Development Guide microblogging example express4.x version of the source code and the preparation process need to pay attention to the problem.
First we look at the current Express version:
This and the express2.x version of the book has changed a lot. For the new features of the EXPRESS4 version, take a look at this: http://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0
Without saying much, we started our journey of creating a project.
First we create a new folder, use CMD to enter the folder, prepare the project. As mentioned in the book, the command to create the project should be:
1 express-t Ejs Microblog
The problem is that the EXPRESS-T parameter has been invalidated and the latest Express version of the default template engine is jade, so in order to use EJS, we need to create the project as follows:
1 express-e Ejs Microblog
As the book says, we run the code directly:
1 Supervisor App.js
and enter Http://localhost:3000/in the browser, do not see the desired effect in the book, but need to app.use in the app.js ('/', routes);
1 App.listen (3000);
2 Console.log (something happening);
Follow the steps in the book and we'll find a problem because in the Views folder it's not Layout.ejs and Index.ejs, because the latest version of Express does not support the Ejs module's Partials method, so it needs to install additional modules on its own:
1 NPM Install Express-partials
Then add the following in the App.js:
var partials = require (' express-partials ');
App.use (Partials ());
Note that this line is added to the App.set (' View engine ', ' Ejs '), and later, if added to App.use ('/', routes), then a CSS reference failure occurs, and the blogger does not understand why.
This is the time to create a new file in the Views Layout.ejs, and then copy the 112-page Layout.ejs code in the book to our new file. Then run the code and you can see the following effect:
The above steps are not a problem, the problem is a series of problems connected to the database. As described below:
In order to do the following, we first need to install the MongoDB database, bloggers recommend this blog: http://be-evil.org/install-mongodb-on-windows7.html
Read a lot of installation MongoDB blog, this is the most effective Bo assertive.
For the new version of Express, according to the book as the database will be connected to the error, the database required to connect the file Settings.js, this according to the book to come to no problem, but the models in the db.js need to make some changes. If you follow the code in the book:
1 var settings = require (' ... /settings ');
2 var Db = require (' MongoDB '). Db;
3 var Connection = require (' MongoDB '). Connection;
4 var Server = require (' MongoDB '). Server;
5 Module.exports = new Db (settings.db, New Server (Settings.host, Connection.default_
6 PORT, {});
The following problems may occur:
After the blogger Google, it was found that the need to write in the following format:
1 var settings = require (' ... /settings '),
2 Db = require (' MongoDB '). Db,
3 Connection = require (' MongoDB '). Connection,
4 Server = require (' MongoDB '). Server;
5 Module.exports = new Db (settings.db, New Server (Settings.host, Connection.default_port, {}), {safe:true});
When referencing the settings module, if you follow the book:
1 var settings = require (' ... /settings ');
will appear:
This is because the latest Express version requires this module to be referenced in this way:
1 var settings = require ('./settings ');
But after solving the problem, one after another, there was this egg-aching situation:
At first I was also unpredictable, but when Google, a friend did a very good answer: http://www.cnblogs.com/yumianhu/p/3709558.html,
That is, in EXPRESS4 we need to install the Express-session package ourselves and then add the reference:
1 var session = require (' express-session ');
The original database reference also needs to be changed to:
1 var mongostore = require (' Connect-mongo ') (session);
and the code:
1 App.use (express.session ({
2 secret:settings.cookie_secret,
3 store:new mongostore ({
4 Db:settings.db
5 })
6}));
It needs to be rewritten as:
1 app.use ({
2 secret:settings.cookie_secret,
3 store:newmongostore ({
4 db:settings.db,
5 })
6 }));
For the view interaction mentioned in the book, the original code is:
1 app.dynamichelpers ({2 user:function (req, res) {3 return Req.session.use
R
4}, 5 error:function (req, res) {6 var err = Req.flash (' error ');
7 if (err.length) 8 return err;
9 else null; Success:function (req, res) {var succ = Req.flash (' success '); if (succ.length) 15
return succ; else return