1. Request and response
The Express application uses arguments to the route callback function:request
andresponse
Object to process request and response data.
app.get('/'.(req, res) = >{... })Copy the code
2. Request object
The REQ object represents an HTTP request and has properties such as the request query string, parameters, body, HTTP table header, and so on. In this document, by convention, the object is always called REq (THE HTTP response is RES), but its actual name is determined by the arguments to the callback function you are using.
Print some attributes of the request as follows:
const express = require('express');
const app = express()
app.get('/'.(req, res) = > {
console.log(req.url);
console.log(req.method);
console.log(req.headers);
console.log(req.query);
res.send('Hello world')
})
app.listen(3000.() = > {
console.log('server is running!! ')})Copy the code
Print result:
/? foo=bar GET {'content-type': 'application/json'.'user-agent': 'PostmanRuntime / 7.28.4'.accept: '* / *'.'postman-token': 'b550c752-78dc-4b36-81c5-658838bb4307'.host: 'localhost:3000'.'accept-encoding': 'gzip, deflate, br'.connection: 'keep-alive'.'content-length': '38'
}
{ foo: 'bar' }
Copy the code
3. Response object
The RES object represents the HTTP response that the Express application sends when it receives an HTTP request. In this article, by convention, the object is always called RES (and the HTTP request is REq), but its actual name is determined by the arguments to the callback function you are using. The request code is as follows:
const express = require('express');
const app = express()
app.get('/'.(req, res) = > {
res.cookie('foo'.'bar')
res.status(201).send('ok')
})
app.listen(3000.() = > {
console.log('server is running!! ')})Copy the code