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't 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 discovered because the express version of the reason for the upgrade, the book is a lot of code can not be used, this 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:
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:
express -e ejs microblog
As the book says, we run the code directly:
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);
app.listen(3000);
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:
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:
var settings = require('../settings');
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
module.exports = new Db(settings.db, new Server(settings.host, Connection.DEFAULT_ PORT, {}));
The following problems may occur:
After the blogger Google, it was found that the need to write in the following format:
var settings = require('../settings'),
Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
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:
var settings = require('../settings');
will appear:
This is because the latest Express version requires this module to be referenced in this way:
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:
var session = require('express-session');
The original database reference also needs to be changed to:
var MongoStore = require('connect-mongo')(session);
and the code:
app.use(express.session({
secret: settings.cookie_secret,
store:new MongoStore({
db: settings.db
})}));
It needs to be rewritten as:
app.use(session({
secret: settings.cookie_secret,
store: newMongoStore({
db : settings.db,
})
}));
For the view interaction mentioned in the book, the original code is:
app.dynamicHelpers({
user: function(req, res) {
return req.session.user;
},
error: function(req, res) {
var err = req.flash('error');
if (err.length)
return err;
else
return null;
},
success: function(req, res) {
var succ = req.flash('success');
if (succ.length)
return succ;
else
return null;
},
});
In the latest version of Express need to change:
app.use(function(req, res, next){
console.log("app.usr local");
res.locals.user = req.session.user;
res.locals.post = req.session.post;
var error = req.flash('error');
res.locals.error = error.length ? error : null;
var success = req.flash('success');
res.locals.success = success.length ? success : null;
next();
});
Flash is used in the registration page, but the latest version of Express does not support Flash, you need to first use NPM install Connect-flash. Then add the following code to the App.js:
app.use(flash());
Then follow the steps of the book to go, basically will not be a problem, and finally we can get the effect we want to:
It is necessary to explain that, following the book to write this small application, there have been a lot of problems, that is, many of the problems encountered, but the blogger is in the spirit of independent research constantly Google, and finally jumped out of the pit of God, got the final effect, I hope to see this blog friends read carefully, also take a good look at the link below , it is probably also the problem you encounter, if you are in the process of writing code with the book encountered problems, Welcome to communicate ~
The above is the entire content of this article, please take a moment to share the article to your friends or leave a comment. We will sincerely thank you for your support!