Express. js is an MVC development framework of nodejs and supports multiple templates such as jade. Next, let's talk about the express app. the configuration content of the js file should first look at this app. js File 01/** 02 * Moduledependencies.03 */0405 varexpressrequire (& amp; #39; e...
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
App. set ('view', _ dirname + '/view ');
2
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 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
App. use (express. bodyParser ());
2
App. use (express. methodOverride ());
3
App. use (app. router );
4
App. use (express. static (_ dirname + '/public '));
Express. bodyParser () is Connect? Why? Iddleware. Set the post request submitted by the client to the request. body.
Express. methodOverride () is also Connect? Why? Pregnant women ???? OST requests disguise 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? Why? Create? Cai ?? Equests, such as css, js, and imgfile. Therefore, files in the specified folder in static () are directly thrown out as static resources.
> App. configure settings
1
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
});
Express. errorHandler () is Connect? Why? Onion ??? Why? U yaoyi step clothing read? Another usage of pp. configure () is that the first parameter is the node. js environment setting, so that we can set different dump in different execution environments. Note: node. js uses NODE_ENV? Environment variables to get environment settings. For example, in the Command column, NODE_ENV = production node app. js can enter the production environment.
> Route and request processing
1
App. get ('/', function (req, res ){
2
Res. render ('index ',{
3
Title: 'express'
4
});
5
});
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
App. post ('/add', function (req, res ){
2
Res. render ('add ',{
3
Sum: req. body. a + req. body. B
4
});
5
});
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
Http. createServer (app). listen (app. get ('Port'), function (){
2
Console. log ("Express server listening on port" + app. get ('Port '));
3 www.2cto.com
});
1
App. set ('Port', process. env. port | 3000 );
Author: YouDoce