Skip to content

Latest commit

 

History

History
117 lines (83 loc) · 2.85 KB

request.md

File metadata and controls

117 lines (83 loc) · 2.85 KB

req

Request prototype.

req.get()

Return request header.

The Referrer header field is special-cased, both Referrer and Referer will yield are interchangeable.

Examples

  req.get('Content-Type');
  // => "text/plain"
  
  req.get('content-type');
  // => "text/plain"
    
  req.get('Something');
  // => undefined

req.accepts()

Check if the given type(s) is acceptable, returning the best match when true, otherwise undefined, in which case you should respond with 406 "Not Acceptable".

The type value may be a single mime type string such as "application/json", the extension name such as "json", a comma-delimted list such as "json, html, text/plain", or an array ["json", "html", "text/plain"]. When a list or array is given the best match, if any is returned.

Examples

  // Accept: text/html
  req.accepts('html');
    // => "html"

  // Accept: text/*, application/json
  req.accepts('html');
  // => "html"
  req.accepts('text/html');
  // => "text/html"
  req.accepts('json, text');
  // => "json"
  req.accepts('application/json');
  // => "application/json"

  // Accept: text/*, application/json
  req.accepts('image/png');
  req.accepts('png');
  // => undefined

  // Accept: text/*;q=.5, application/json
  req.accepts(['html', 'json']);
  req.accepts('html, json');
  // => "json"

req.acceptsCharset()

Check if the given charset is acceptable, otherwise you should respond with 406 "Not Acceptable".

req.acceptsLanguage()

Check if the given lang is acceptable, otherwise you should respond with 406 "Not Acceptable".

req.param()

Return the value of param name when present or defaultValue.

  • Checks body params, ex: id=12, {"id":12}
  • Checks route placeholders, ex: /user/:id
  • Checks query string params, ex: ?id=12

To utilize request bodies, req.body should be an object. This can be done by using the connect.bodyParser() middleware.

req.is()

Check if the incoming request contains the "Content-Type" header field, and it contains the give mime type.

Examples

   // With Content-Type: text/html; charset=utf-8
   req.is('html');
   req.is('text/html');
   req.is('text/*');
   // => true
  
   // When Content-Type is application/json
   req.is('json');
   req.is('application/json');
   req.is('application/*');
   // => true
  
   req.is('html');
   // => false

Now within our route callbacks, we can use to to assert content types such as "image/jpeg", "image/png", etc.

   app.post('/image/upload', function(req, res, next){
     if (req.is('image/*')) {
       // do something
     } else {
       next();
     }
   });