Document directory
- Set the views path and template
- App. Use Configuration
- App. configure settings
- Route and request processing
- Add listen and start the nodejs Server
Original article address:Http://js8.in/774.html
Express. jsIt is an MVC development framework of nodejs and supports multiple templates such as Jade. Next, let's talk about the installation of express and the configuration of APP. js files. In the future tutorial, we will use Express. js to build a chat room step by step.
Install Express. js
If you have installed NPM, the installation becomes simple. You only need to run the following code in the terminal:
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:
NPM list-G
NPM list
If there is no NPM, I can use GitHub to git down the latest Express.
Now you can useexpress testapp
To create an express instance. The following is an example:
Cd ~
Express testapp
CD testapp
Node app. js
In this way, a testapp'sNodejsApp. JS is the default main application Js. The following describes the configuration in APP. js in detail.
Introduction module
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:
app.set('views', __dirname + '/views');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.
Whileapp.set('view engine', 'jade');
Set the render engine used by 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
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
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:
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:
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
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 understoodExpress ConfigurationThen, you won't write a hello World with others as before, but you don't know the meaning of each line of code.