Express. js is an MVC development framework of nodejs and supports multiple templates such as jade. The following describes the configuration content of the express app. js file.
Let's take a look at this app. js file.
01
/**
02
* Module dependencies.
03
*/
04
05
Var express = require ('express ')
06
, Routes = require ('./routes ')
07
, Http = require ('http ');
08
09
Var app = express ();
10
11
App. configure (function (){
12
App. set ('Port', process. env. port | 3000 );
13
App. set ('view', _ dirname + '/view ');
14
App. set ('view engine ', 'jade ');
15
16
App. use (express. favicon ());
17
App. use (express. logger ('dev '));
18
App. use (express. bodyParser ());
19
App. use (express. methodOverride ());
20
App. use (app. router );
21
App. use (express. static (_ dirname + '/public '));
22
});
23
24
App. configure ('development ', function (){
25
App. use (express. errorHandler ());
26
});
27
28
App. get ('/', routes. index );
29
30
Http. createServer (app). listen (app. get ('Port'), function (){
31
Console. log ("Express server listening on port" + app. get ('Port '));
32
});
> Introduction module
1
Var express = require ('express ');
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, express, in the $ NODE_PATH and node_modules folder under the current js directory. js inherits from the connect module. Therefore, if you do not have the connect module in the node_modules folder.
> Set the views path and template
1
<B> app. set ('view', _ dirname + '/view ');
2
App. set ('view engine ', 'jade'); </B>
The above two rows set the views folder, that is, the template folder, __dirname is node. the global variable in js is the path where the executed js is located, and the _ dirname 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 jQuerytemplate.
> App. use Configuration
1
<B> app. use (express. bodyParser ());
2
App. use (express. methodOverride ());
3
App. use (app. router );
4
App. use (express. static (_ dirname + '/public'); </B>
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 Connect middleware 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
1
<B> app. configure ('development ', function (){
2
App. use (express. errorHandler ({dumpExceptions: true, showStack: true }))
3
;
4
});
5
App. configure ('production ', function (){
6
App. use (express. errorHandler ());
7
}); </B>
Express. errorHandler () is the middleware built in Connect to assist in handling exceptions. The app is also exposed here. another use of configure (), the first parameter is node. js environment settings, so we can set different dump in different execution environments. Note: node. js uses the NODE_ENV environment variable to get environment settings. e.g.: In the command column, NODE_ENV = production node app. js can enter the production environment.
> Route and request processing
1
<B> app. get ('/', function (req, res ){
2
Res. render ('index ',{
3
Title: 'express'
4
});
5
}); </B>
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:
1
<B> app. post ('/add', function (req, res ){
2
Res. render ('add ',{
3
Sum: req. body. a + req. body. B
4
});
5
}); </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 to start the nodejs server.
1
<B> http. createServer (app). listen (app. get ('Port'), function (){
2
Console. log ("Express server listening on port" + app. get ('Port '));
3 www.2cto.com
}); </B>
1
App. set ('Port', process. env. port | 3000 );
Author: YouDoce