English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Express is a simple and flexible node.js Web application framework that provides a series of powerful features to help you create various Web applications and rich HTTP tools.
Express can quickly build a fully functional website.
Core features of the Express framework:
Middleware can be set up to respond to HTTP requests.
A routing table is defined to execute different HTTP request actions.
HTML pages can be dynamically rendered by passing parameters to the template.
Install Express and save it to the dependency list:
$ cnpm install express --save
The above command will install the Express framework into the node_modules directory of the current directory, and the express directory will be automatically created under node_modules.
body-parser - A middleware in node.js, used to handle JSON, Raw, Text, and URL-encoded data.
cookie-parser - This is a tool for parsing cookies. The cookies that are passed can be retrieved using req.cookies and converted into an object.
multer - node.js middleware, used to handle enctype="multipart/form-data" (sets the MIME encoding for form data).
$ cnpm install body-parser --save $ cnpm install cookie-parser --save $ cnpm install multer --save
After installation, we can check the version number of express used:
$ cnpm list express /data/www/node └── [email protected] -> /Users/tianqixin/www/node/node_modules/.4.15.2@express
Next, we use the Express framework to output "Hello World".
In the following example, we have imported the express module and responded with the string "Hello World" after initiating a request from the client.
Create an express_demo.js file, the code is as follows:
express_demo.js file code: //express_demo.js file var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); ) var server = app.listen(8081, function () { var host = server.address().address var port = server.address().port console.log("Application instance, access address is http://%s:%s, host, port) )
Execute the following code:
$ node express_demo.js Application instance, access address is http://0.0.0.0:8081
Access in the browser http://127.0.0.1:8081, the result is as follows:
Hello World