Home >Web Front-end >JS Tutorial >nodejs tutorial detailed steps to install express and configure app.js file_json

nodejs tutorial detailed steps to install express and configure app.js file_json

WBOY
WBOYOriginal
2016-05-16 17:34:091277browse

Install express.js

If you have npm installed, the installation is very simple, just run the following code in the terminal:

Copy the code The code is as follows:

npm install express -gd

-g means to install it into the lib of NODE_PATH, and -d means to install the dependency package together. If there is no -g, the current directory will be installed (a node_modules folder will be created). You can compare the difference between the two through the following command:

Copy code The code is as follows:

npm list -g
npm list

If there is no npm, then I can use github to git download the latest express.
Okay, now you can create an express instance through express testapp. Here is an example:

Copy code The 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 application main js. Let’s talk about the various configurations in app.js in detail.

Introduce module

Copy code The code is as follows:

var express = require('express');
var app = module.exports = express.createServer();

require() is a function provided by node.js, which allows you to introduce other modules to call the functions and variables of the module. By default, node.js will search for modules in $NODE_PATH and the node_modules folder in the current directory where js is located. . require can also be used to load modules written by yourself~ This involves the module mechanism of node.js, which I will introduce later when I have the opportunity.

The express.createServer() in the second line is to establish the server, and the module.exports in the middle also involves the module mechanism of node.js, which will be discussed later.

Detailed configuration instructions for express’s app.js

express.js inherits from the connect module, so it won’t work if there is no connect module in your node_modules folder.

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

Copy the code The 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 in node.js, which is the path where the executed js is obtained. In addition, __filename is the name of the currently executed js file. Therefore, app.set(‘views’, __dirname ‘/views’); is the folder where views are set.

And app.set('view engine', 'jade'); is to 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

Copy code The 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 Connect’s built-in middleware. Setting this setting can put the post request submitted by the client into request.body.
express.methodOverride() is also built-in to Connect and can help handle POST requests disguised as PUT, DELETE and other HTTP methods.
app.router() is route requests, but the official document of express.js says that this sentence is dispensable, and after testing, it is true, but it is still written.
express.static() is also a built-in middleware of Connect to handle static requests, such as css, js, img files, etc. Therefore, the files in the folder specified in static() will be spit out directly as static resources.

app.configure settings

Copy code The 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 Connect’s built-in middleware to help handle exceptions. This also reveals the first usage of app.configure(). The first parameter is the environment setting of node.js, so that we can set different levels of dump in different execution environments. PS: node.js obtains environment settings through the NODE_ENV environment variable. e.g.: In the command line, NODE_ENV=production node app.js can enter the production environment.

Routing and request processing

ok, here is the content of nodejs processing request:

Copy code The code is as follows:

app.get('/', function(req, res){
res.render('index', {
title: 'Express'
});
});

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

To handle post requests in express, you need to use app.post(). Such as the following code:

Copy code The 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 the POST parameters.

In addition to the get and post methods, there is also app.all(), which means all request processing.

Add listen and start the nodejs server

Copy code The 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 understood the express configuration, and we will not write hello world with others like before without knowing the meaning of each line of code.

Reprinted from JS8.IN ™

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn