Important concepts
Simply put, what the Web server needs to do is to read the request –> write the response. According to the HTTP protocol, the request data includes the request row, the request head and the request body. The response data includes the response row, the response head and the response. The purpose of the Web framework is to help us read requests and set responses more easily and efficiently.
The most important concept of Express is middleware. The core API of Express is app.use(). You could even say That Express is a middleware library. The request comes in and is “processed” by one middleware function after another, and the response can be completed in any “processed” process, or next() can be used to proceed to the next middleware.
Take a look at the official Hello World example:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World! '))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Copy the code
You can see why use middleware is not used. Express is all about use. In fact, this code is equivalent to writing:
const express = require('express')
const app = express()
const port = 3000
app.use((req, res) => {
if(req.path === "/" && req.method === "GET") {
res.send('Hello World! ')
}
})
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Copy the code
App.get () is also just syntax candy, and if you don’t see Express as a middleware library, it’s probably because there are too many syntax candy based on app.use, such as app.post(), app.route(), etc. In fact, the routing encapsulation in Express itself adds a layer of request path processing and judgment on the basis of middleware.
Many Express-based libraries, whenever called through use, return a middleware. For example, express is express.static
app.use(express.static(path.join(__dirname, 'public')));
Copy the code
Express.static () might return something like this (purely a shot in the dark) :
(req, res, next) => {
if(Request path match) {// Read file and respond}else {
next()
}
}
Copy the code
The middleware model makes it easy to add functionality to applications and also helps third-party developers enrich the Express ecosystem.
Here’s a quick look at the Express API documentation and note the important points.
express
express.json()
Parse the data into JSON. For example, parsing the Application/JSON type, which is a JSON string, will eventually be parsed to JSON
express.urlencoded()
Handles data like Application/X-www-form-urlencoded type (which is transmitted as key=xxx&name=111) and parses it into objects
express.raw()
Parse the request body into a Buffer
express.text()
Parse the request body into a string
Summary: All of the above interfaces process the data of the request body, depending on the content-Type (or custom) of the request. For example, express.json() defaults to parse requests whose content-type is “application/json”. If the content-type is “application/json” but the request body is a normal string, parsing errors will occur. In addition, the requested data is transmitted as a stream. Using middleware like this, you can get the required data directly from request.body. Otherwise, you have to read the stream data manually and parse it.
express.static()
Static resources can be mounted. Note: If the access file does not exist, it is forwarded to the following request processing middleware:
const express = require('express')
const app = express()
app.use(express.static('static'))
app.use((request, response, next) => {
response.end('hi') next() }) app.listen(3000, () => console.log(`App listening on port 3000! `))Copy the code
The above code will return hi if the file does not exist, or return the file if it does. (Middleware again)
Application
-
App.set (configurable template engine)
-
App.get (app.set set value)
-
The app.locals global variable
-
app.use
-
App. Post/app. Put/app. Get and so on
-
app.on(‘mount’, ()=>{})
Request
BaseUrl, req.body, req.cookies, req.hostname, req. Method, req.query, etc. A few things to note:
- Req.fresh: The browser negotiates the cache. Check whether the browser cache can be used based on the request header and response header
- Req. range: related to file fragment downloads
Response
Location Redirects with the 301 status code
Format responds to different data depending on the accept of the request
Attachment helps you set the Content-Disposition response header for download
Send Indicates sending or writing a stream at a time
Write more rough, use later and then slowly add it
Other articles:
-
Streams in node
-
JavaScript asynchronously goes from promise to await
-
Closures in JavaScript