Install express. js
If you have installed npm, the installation becomes simple. You only need to run the following code in the terminal:
Copy codeThe Code is as follows:
Npm install express-gd
-G indicates to install it in the lib of NODE_PATH, and-d indicates to install dependency suites together. If no-g exists, the current directory will be installed (A node_modules folder will be created). You can use the following command to compare the differences between the two:
Copy codeThe Code is as follows:
Npm list-g
Npm list
If there is no npm, I can use github to git down the latest express.
Now you can use express testapp to create an express instance. The following is an example:
Copy codeThe Code is as follows:
Cd ~
Express testapp
Cd testapp
Node app. js
In this way, a nodejs application of testapp is created, and app. js is the default main application js. The following describes the configuration in app. js in detail.
Introduction module
Copy codeThe Code is as follows:
Var express = require ('express ');
Var app = module. exports = express. createServer ();
Require () is a node. the functions provided by js allow you to introduce other modules to call the functions and variables of the modules. By default, node. js will find the module in the $ NODE_PATH and node_modules folder under the current js directory. Require can also be used to load self-written modules ~ This involves the module mechanism of node. js, which will be introduced later.
The express. createServer () in the second line is to establish a server, and the module. exports in the middle is also a module mechanism involving node. js.
Detailed configuration of express app. js
Express. js is inherited from the connect module. Therefore, if you do not have the connect module in the node_modules folder.
Set the views path and template
Let's take a look at the following two lines:
Copy codeThe Code is as follows:
App. set ('view', _ dirname + '/view ');
App. set ('view engine ', 'jade ');
The above two rows set the views folder, that is, the template folder, __dirname is node. the global variable in js is the path of the js to be executed, and the _ filename is the currently executed js file name. Therefore, app. set ('view', _ dirname + '/view'); is the folder for setting views.
App. set ('view engine ', 'jade') is the render engine used to set express. js. In addition to Jade, express. js also 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 the middleware built in Connect. You can set the post request submitted by the client to be placed in request. body.
Express. methodOverride () is also built in Connect. It can assist in processing POST requests in disguise of PUT, DELETE and other HTTP methods.
App. router () is route requests, but the official file of express. js is dispensable. It is true after testing, but write it down.
Express. static () is also a middleware built in Connect to process static requests, such as css, js, and imgfiles. Therefore, files in the specified folder in static () are directly thrown 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 the middleware built in Connect to handle exceptions. The app is also exposed here. configure () is used. The first metric is node. js runtime settings, so that we can set different levels of dump in different runtime environments. PS: node. js uses the NODE_ENV environment change to get the environment settings, e.g.: In the command column, NODE_ENV = production node app. js can be imported into the production environment.
Route and request processing
OK. The content of the request processed by nodejs is as follows:
Copy codeThe Code is as follows:
App. get ('/', function (req, res ){
Res. render ('index ',{
Title: 'express'
});
});
The code above indicates that the index template in the views folder is called in the get request root directory, and the input parameter title is "Express". This title can be directly used in the template file.
To process post requests in express, you must use app. post (). 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
});
});
As mentioned above, req. body is the result after express. bodyParser () processes the POST parameter.
Besides the get and post methods, app. all () means that all requests are processed.
Add listen and 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 completely understood the express configuration, so we will not write a hello world with others as before, but do not know the meaning of each line of code.