Nodejs Development Microblogging Example

Source: Internet
Author: User
Tags mongodb require

This article to share is the use of node.js to achieve the development of micro-blog instances, mainly after reading the Nodejs Development Guide found that many of the code has not been used, the reason is the Express version of the upgrade, so today try to develop micro-blog instances also promoted subordinates, recommended to everyone.

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 beginning to choose the "simple Nodejs" this book, read a few chapters, come to a conclusion is: Really a good book, but still can not write nodejs! and then chose another textbook "Nodejs Development Guide", because read the " In simple Nodejs, skip the first few chapters of the book and write the fifth chapter of the book's microblog examples. 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 ~

This is the whole article, please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.