Install express.js
If you have npm installed, the installation is very simple, just run the following code in the terminal:
-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:
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:
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
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:
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
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
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:
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:
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
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 ™