Nodejs:express 4.x

Source: Internet
Author: User
Tags install mongodb

Recently Learning Express, the latest version to 4.x, compared with the previous version of the 3.0, the change is still very large. Many of the tutorials on the Web are based on 3.0, and then compile an example, and eat a lot of hardships. Now summarize the changes of express4.x.

    1. Installation:

Installation is needless to say, if you do not want to use the latest version, install the previous version, $ npm install-g [email protected]

4.0 and 3.0 specific differences, look at the migrating from 3.x to 4.x documentation will understand.

If the NPM install-g Express command was installed, however, after the installation was successful, the express was not an internal or external command. The original express4.0 version of the command tool was split out (Project address: https://github.com/expressjs/generator), so we also need to install a command tool, the command is as follows:

NPM install-g Express-generator

Create a project using Express:
Express HelloWorld
There have been some changes in the command in the new version, and after creating project, you need to add dependencies and launches with NPM:
CD HelloWorld
NPM Install
NPM start
The newly created HelloWorld is then already running on Port 3000.

Note that the latest boot mode is changed to NPM start instead of node app.js

2. Session

Prior to express 4.0, middleware such as session was automatically installed with Express, and the Express version of the online tutorials was 4.0 versions earlier, so be sure to pay attention to this when using 4.0 and later versions.

First add it in the Package.json.

"Dependencies": {
"Express": "~4.9.0",
"Body-parser": "~1.8.1",
"Cookie-parser": "~1.3.3",
"Morgan": "~1.3.0",
"Serve-favicon": "~2.1.3",
"Debug": "~2.0.0",
"Ejs": "~0.8.5",
"MongoDB": "*",
"Connect-mongo": "*",
"Connect-flash": "*",
"Express-session": "*",
"Cookie-session": "*",
"Cookies": "*"
}

Then use NPM istall to download and install MongoDB, Connect-mongo, Connect-flash, Express-session.

Viewing node_modules\connect-mongo\readme.md can see that the usage after 4.0 has been different.

With EXPRESS4:varSession = require (' express-session '); varMongostore = require (' Connect-mongo ')) (session); App.use (Session ({Secret:settings.cookie_secret, store:NewMongostore ({db:settings.db,})}); With Express<4:        varExpress = require (' Express '); varMongostore = require (' Connect-mongo ')) (Express); App.use (Express.session ({secret:settings.cookie_secret, store:NewMongostore ({db:settings.db})}); With Connect:varConnect = require (' Connect '); varMongostore = require (' Connect-mongo ') (connect);

Adding a session reference to the project, the original version is not required.

var session = require (' express-session ');

var Mongostore = require (' Connect-mongo ') (session);

Instead of using Express.session, use the session directly ():

App.use (Session ({  Secret:settings.cookieSecret,  key:settings.db,//cookie Name Cookie: {maxage:1000 * * * * per * + *  ,//days  new  mongostore ({    D B:settings.db  } ));

Nodejs:express 4.x

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.