Nodejs Express 4.X Chinese API 2---request Chapter

Source: Internet
Author: User

Related reading:

Express 4.X API Translator [i]--application

Express 4.X API translation [II]-Request Chapter

Express 4.X API translation [III]---response

Express 4.X API translation [IV]---router

Req.params

This is an object property that contains the named routing rule parameter. For example, if you have a routing rule that says "/user/:name", then this "name" property can be called using Req.params.name, which defaults to {}

1 // GET/USER/TJ 2 Req.params.name 3 // = "TJ"

When a regular expression is used when defining a routing rule, the matching result is provided in the array using Req.params[n], where n refers to the nth matched array. Such rules are applied in the use of unnamed wildcard matching rules, for example /file/* ;

1 // Get/file/javascript/jquery.js 2 req.params[0]3//  = "Javascripts/jquery.js"

Req.query

This property is an object property containing the parsed request parameter object, which defaults to {}

1 //Get/search?q=tobi+ferret2 req.query.q3 //= "Tobi Ferret"4  5 //Get/shoes?order=desc&shoe[color]=blue&shoe[type]=converse6 Req.query.order7 //= "desc"8 Req.query.shoe.color9 //= "Blue"Ten Req.query.shoe.type One //= "Converse"

Req.body

This property contains a parsed request body. This feature is provided by the middleware Bodyparser, although other request body parsing middleware will support such conventions well. This property is defined as {} when Bodyparser () is used.

 1  //  2  req.body.user.name  3  // => "Tobi"  req.body.user.email   5  //  6  //  post {"name": "Tobi"}  7  req.body.name  8  // => "Tobi"  

Req.param (name)

Returns a value with a parameter named name

1 //? Name=tobi2Req.param (' name ');3 //= "Tobi"4  5 //POST Name=tobi6Req.param (' name ')7 //= "Tobi"8  9 ///user/tobi The corresponding routing rule is/user/:nameTenReq.param (' name ') One //= "Tobi"

The priority of the lookup is as follows:

    • Req.params
    • Req.body
    • Req.query

Direct access to Req.body, Req.params, and req.query should be more clear unless you really need to accept input from each object.

Req.route

The currently matched "route" contains properties such as the original string for the route, the converted regular expression, and so on.

1 app.get ('/user/:id? ',function(req,res) {2    console.log (req.route); 3 });

The example above will output the following:

12   path: '/user/:id? ' , 3   Keys: [{name: ' id ', Optional:true}],4   regexp:/^\/user (?: \ /([^\/]+?))? \/?$/I,5   params:[id: '6 }

req.cookies

When the Cookieparser () middleware is used, the object will be initialized to {}, and in addition, the cookies sent by the user agent are included.

1 // COOKIE:NAME=TJ 2 Req.cookies.name 3 // = "TJ"

If you have any questions or concerns, please refer to Cookie-parser's additional documentation

req.signedcookies

When the Cookieparser (secret) middleware is executed, the object is initialized to {} and contains the signed cookies sent by the user agent, unsigned and ready to use. The signed cookie is stored in a separate object, otherwise the attacker would easily replace the value in "Req.cookie". It is important to note that signed cookies are not with the table they are hidden or encrypted, and this is only the same as preventing tampering with cookies.

1 // Cookie:user = Tobi. Cp7awaxdfakirfh49dqzkjx7skzzsopq7/acbbrvwli32req.signedCookies.user3//  = "Tobi"

If you have any questions or concerns, please refer to Cookie-parser's additional documentation

req.get (field)

Gets the field fields within the request header, which are not case sensitive, where the referrer and Referer fields are interchangeable.

1 req.get (' Content-type '); 2 // = "Text/plain" 3 req.get (' Content-type '); 4 // = ' Text/plain ' 5 req.get (' Something '); 6 // = undefined

Alias is req.header (field);

req.accepts (types)

Checks whether the given type types is an acceptable type, returns the best match when it is an acceptable type, otherwise returns undefined– in this case, you should return 406″not acceptable ".

The value of type can be a single string of type mine, such as "Application/json", with the extension "JSON", or a comma-delimited list or array. A best bet is returned when it is a list or an array.

1 //accept:text/html2Req.accepts (' HTML ');3 //= "html"4 //accept:text/*, Application/json5Req.accepts (' HTML ');6 //= "html"7Req.accepts (' text/html ');8 //= "text/html"9Req.accepts (' JSON, text ');Ten //= "JSON" OneReq.accepts (' Application/json '); A //= "Application/json" - //accept:text/*, Application/json -Req.accepts (' Image/png '); theReq.accepts (' PNG '); - //= undefined - //accept:text/*;q=.5, Application/json -Req.accepts ([' HTML ', ' JSON ']); +Req.accepts (' HTML, JSON '); - //= "JSON"

If you have any questions or concerns, please refer to accepts's additional documentation

Req.acceptscharset (CharSet)

Checks whether a given charset is acceptable

If you have any questions or concerns, please refer to accepts's additional documentation

req.acceptslanguage (lang)

Checks whether a given Lang is an acceptable

If you have any questions or concerns, please refer to accepts's additional documentation

req.is (type)

Check that the submitted request contains the "Content-type" header field and that he matches the given MIME Type

1 //With content-type:text/html; Charset=utf-82Req.is (' HTML ');3Req.is (' text/html ');4Req.is (' text/* ');5 //= True6 //When Content-type is Application/json7Req.is (' JSON ');8Req.is (' Application/json ');9Req.is (' application/* ');Ten //= True OneReq.is (' HTML '); A //= False

If you have any questions or concerns, please refer to Type-is's additional documentation

Req.ip

Returns the remote address, or returns the upstream IP address when the reverse proxy is enabled.

1 Req.ip 2  // = 127.0.0.1

req.ips

When the reverse proxy mode is turned on, the "x-forwarded-for" IP Address list is parsed and an array is returned, otherwise an empty array is returned. For example, if a value is "Client,proxy1,proxy2″ you will receive an array [" Client "," Proxy1 "," Proxy2 "] here you can see" Proxy2″ is the furthest downstream address. "

Req.path

Returns the URL path name of the request.

1 // Example Com/users?sort=desc 2 Req.path 3 // = "/users"

Req.host

Returns the host name taken from the "host" request, but does not contain a port number.

1  // Host: "example.com:3000" 2 Req.host 3  // = "example.com"

Req.fresh

Check whether the request is new – by matching the last-modified or ETag to indicate whether the resource is "new".

1     Req.fresh 2     // =>true

Req.stale

Check that the request is not old – if the last-modified or ETag does not match, indicate that the resource is old.

1     req.stale 2     // =>true

REQ.XHR

This header is set when checking for a field with "X-requested-with" in the request head and a value of "XMLHttpRequest" (jQuery, etc.) request

1     req.xhr 2     // = True

Req.protocol

The request protocol string "http" or "https" is returned when the request is TLS. When the reverse proxy is enabled, the "X-forwarded-proto" request header will be trusted. If you run a reverse proxy that supports the HTTPS protocol, this will be supported.

1     req.protocol 2     // = "http"

req.secure

Check to see if the TLS connection has been established. Here is a shorthand

1     ' HTTPS ' = = Req.protocol

Req.subdomains

Returns an array of sub-domains

1 // Host: "Tobi.ferrets.example.com" 2 Req.subdomains 3 // =>[' ferrets ', ' Tobi ']

Req.originalurl

This property is much like Req.url, however, he retains the URL of the original request, allowing you to freely rewrite req.url while doing internal routing. For example, App.use () overrides Req.url as a mount point

1     // get/search?q=something 2     req.originalurl 3     // = '/search?q=something '

Transferred from: http://www.90it.net/expressjs-4-api-zh-cn-request.html

Nodejs Express 4.X Chinese API 2---request Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.