Nodejs Tutorial Installing Express and configuring App.js files in Detailed steps

Source: Internet
Author: User

From: Http://www.jb51.net/article/36710.htm Express.js is an MVC development framework for NODEJS and supports a variety of templates such as Jade. The following is a simple example of express installation and App.js file configuration, and then in the next tutorial step by step using express.js to build a chat room

Installing Express.js

If you install NPM, the installation becomes simple, just run the following code in the terminal:

Copy CodeThe code is as follows:
NPM Install EXPRESS-GD

-G represents the installation to the Lib in Node_path, and the-D represents the installation of the dependency kit. If you do not have-G will install the current directory (will create a node_modules folder), you can use the following instructions to compare the difference between the two:

Copy CodeThe code is as follows:
NPM list-g
NPM List

Without NPM, I could use GitHub to git down to the latest express.
OK, now you can create an express instance with express TestApp. The following is an example:

Copy CodeThe code is as follows:
CD ~
Express TestApp
CD TestApp
Node App.js

This establishes a TestApp Nodejs application, and App.js is the default application Master JS. The following is a detailed talk about the various configurations in App.js.

Introducing Modules

Copy CodeThe code is as follows:
var express = require (' Express ');
var app = Module.exports = Express.createserver ();

Require () is a function provided by node. js that allows you to introduce other modules to invoke the functions and variables of the module, and by default, node. JS will look for the module under the Node_modules folder in the directory where $node_path and current JS are located. Require can also be used to load their own modules Oh ~ This involves node. JS's module mechanism, the following opportunity is introduced.

The second line of Express.createserver () is the establishment of the server, and the middle of the module.exports is also related to node. JS module mechanism, and later.

Detailed configuration instructions for Express App.js

Express.js inherits from the Connect module, so it is not possible to have a connect module in your Node_modules folder.

Set the views path and template
Let's look at the following two lines:

Copy CodeThe code is as follows:
App.set (' views ', __dirname + '/views ');
App.set (' View engine ', ' Jade ');

The above two lines are to set the Views folder, that is, the template folder, __dirname is the global variable node. js, that is, the path to execute the JS, and the other __filename is currently executed JS file name. So, app.set (' views ', __dirname + '/views ') is a folder for setting views.

and App.set (' View engine ', ' Jade ') is the render engine used to set up the express.js. In addition to Jade, Express.js supports JS templates such as Ejs (embedded JavaScript), Haml, Coffescript, and jquery template.

App.use Configuration

Copy CodeThe code is as follows:
App.use (Express.bodyparser ());
App.use (Express.methodoverride ());
App.use (App.router);
App.use (express.static (__dirname + '/public '));

Express.bodyparser () is a middleware built into connect, where you can place a POST request submitted by the client in Request.body.
Express.methodoverride () is also a connect built-in that can assist with post requests masquerading put, delete, and other HTTP methods.
App.router () is the route requests, but Express.js's official file is this one, and after testing it really is, but write it.
Express.static () is also a connect built-in middleware to handle static requests, such as CSS, JS, img files, and so on. So the files in the folder specified in static () are directly spit out as static resources.

App.configure settings

Copy CodeThe code is as follows:
App.configure (' development ', function () {
App.use (Express.errorhandler ({dumpexceptions:true, showstack:true}));
});
App.configure (' Production ', function () {
App.use (Express.errorhandler ());
});

Express.errorhandler () is a middleware in connect to help with the exception. This also exposes the use of app.configure (), the first of which is node. JS's environment, so we can set a different level of dump in different environments. PS:node.js is an environmental setting through node_env, e.g.: In the command line,node_env=production node App.js can enter the production environment .

Handling of Routing and request

OK, here's what Nodejs processing the request:

Copy CodeThe code is as follows:
App.get ('/', function (req, res) {
Res.render (' index ', {
Title: ' Express '
});
});

The above code means that the GET request root directory is called the index template in the Views folder, and the incoming parameter title is "Express", this title can be used directly in the template file .

To process the POST request in Express, you need to use App.post (). As in the following code:

Copy CodeThe code is as follows:
App.post ('/add ', function (req,res) {
Res.render (' Add ', {
SUM:REQ.BODY.A + req.body.b
});
});

We mentioned earlier that req.body is the result of express.bodyparser () processing post parameters .

In addition to the get and post methods, there is app.all () meaning that all requests are processed .

Add listen to start the Nodejs server

Copy CodeThe code is as follows:
App.listen (3000);
Console.log (
"Express server listening on port%d in%s mode",
App.address (). Port,
APP.SETTINGS.ENV);

So far, we have basically fully understood the express configuration, it will not be like before with others to write a Hello world but do not know the meaning of the lines of code.

Nodejs Tutorial Installing Express and configuring App.js files in Detailed steps

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.