Request object in Node.js
The req object represents the HTTP Request and has properties for the request query string, parameters, body, HTTP Header, .
Before entering the chapter, we will learn what the middleware concept is:
According to vi.wikipedia: Middleware is computer software that connects software components or applications together. This type of software includes a set of services that allow interaction between processes running on one or more machines.
Properties of the Request object in Node.js
The table below lists some commonly used properties of the Request object in Node.js:
Stt Properties & Description1 req.appThis property holds a reference to the Express application using Middleware 2 req.baseUrl
The URL link on which a router is mounted 3 req.body
Contains key-value pairs of data submitted in the body section of the Request. By default, it is undefined, and is generated when you use a Middleware to parse the body of the request ( body-parser example) 4 req.cookies
When using cookie-parser middleware, this attribute is an object that contains Cookies sent by Request 5 req.fresh
Determine whether or not Request is "fresh." This attribute is in contrast to req.stale. 6 req.hostname
Contains the hostname section from the "Host" section of the HTTP header. 7 req.ip
Remote IP address of the request. 8 req.path
Contains the path part of the URL in the Request. 9 req.protocol
The protocol string of Request, which is "http" or "https" when requested with TLS 10 req.query
An object contains an attribute for each query string parameter in Router 11 req.route
A string representing the current route is connected to 12 req.secure
Is true if a TLS connection is successfully established 13 req.signedCookies
When using cookie-parser Middleware, this property contains Cookies sent by Request 14 req.stale
Determining whether or not Request is "stale", this attribute is in contrast to the req.fresh attribute. 15 req.subdomains
An array consists of subdomains in the domain name section of the Request
Method of Request object in Node.js
Method req.accepts (types)
req . accepts ( types )
This method checks whether the Content-type is acceptable, based on the Request HTTP Header field of the Request. For example:
// Accept: text/html req . accepts ( 'html' ); // => "html" // Accept: text/*, application/json req . accepts ( 'html' ); // => "html" req . accepts ( 'text/html' ); // => "text/html"
Method req.get (field)
req . get ( field )
This method returns the specific Header field of the Request. For example:
req . get ( 'Content-Type' ); // => "text/plain" req . get ( 'content-type' ); // => "text/plain" req . get ( 'Something' ); // => undefined
Method req.is (type)
req . is ( type )
This method returns true if the Content-Type field of the Request is connected to the Type MIME type specified by the type parameter. For example:
// With Content-Type: text/html; charset=utf-8 req . is ( 'html' ); req . is ( 'text/html' ); req . is ( 'text/*' ); // => true
Req.param method (name [, defaultValue])
req . param ( name [, defaultValue ])
This method returns the value of the name parameter. For example:
// ?name=tobi req . param ( 'name' ) // => "tobi" // POST name=tobi req . param ( 'name' ) // => "tobi" // /user/tobi for /user/:name req . param ( 'name' ) // => "tobi"
According to Tutorialspoint
Previous article: RESTful API in Node.js
Next lesson: Response object in Node.js
You should read it
May be interested
- How to Write Your First Program in Javajava is an object-oriented programming language created in 1995 by james gosling, which means that it represents concepts as 'objects' with 'fields' (which are attributes that describe the object) and 'methods' (actions that the object can...
- Class (class) and Object in C ++the main purpose of c ++ is to add object orientation to the c programming language and classes which are central features of c ++ that support object-oriented programming and are often called types user defined (user-defined).
- The most dangerous object in the world, only 5 minutes of exposure can cause deaththis object is located in the chernobyl area, ukraine, as a result of one of the most horrifying radiation leaks in history. this object is shaped like a giant foot, so it is called the elephant's foot.
- Document Object Model (DOM) in JavaScripteach site resides within a browser window that can be viewed as an object.
- Objects in JavaScriptjavascript is an object oriented programming language (object oriented programming). a program language can be called object-oriented if it provides four basic capabilities to the programmer.
- The certreq command in Windowscertreq can be used to request a certificate from a certificate authority (ca), in order to receive a response to a previous request from a ca, creating a new request from the .inf file.
- Object-oriented programming in Pythonpython is a powerful object-oriented programming language. therefore, creating and using objects is very easy. this article will introduce some basic concepts in object-oriented programming, as well as how to create and use them.
- Object projector and things to knowprojectors are becoming increasingly popular and are widely used in the fields of life and work.
- How to quickly fix device descriptor request failed errorconnected usb to computer but computer does not recognize it. this is a sign of a device descriptor request failed error. to fix the device descriptor request failed error, read tipsmake's article immediately!
- Object from unknown location continuously emitting radio wavesthe object sgr 0501+4516 is so strange that scientists believe it may hold the secret to the phenomenon of radio bursts.