request.md
This file
Path
docs/request.md
117 lines · 2.8 KiB · Markdown
Last changed by
Added new dox API docsTJ Holowaychukbefore adoption7e81cad
History of this fileTake it
Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 1–117why, one step back12# req34 Request prototype.56# req.get()78 Return request header.9 10 The `Referrer` header field is special-cased,11 both `Referrer` and `Referer` will yield are12 interchangeable.13 14 ## Examples15 16 req.get('Content-Type');17 // => "text/plain"18 19 req.get('content-type');20 // => "text/plain"21 22 req.get('Something');23 // => undefined2425# req.accepts()2627 Check if the given `type(s)` is acceptable, returning28 the best match when true, otherwise `undefined`, in which29 case you should respond with 406 "Not Acceptable".30 31 The `type` value may be a single mime type string32 such as "application/json", the extension name33 such as "json", a comma-delimted list such as "json, html, text/plain",34 or an array `["json", "html", "text/plain"]`. When a list35 or array is given the _best_ match, if any is returned.36 37 ## Examples38 39 // Accept: text/html40 req.accepts('html');41 // => "html"42 43 // Accept: text/*, application/json44 req.accepts('html');45 // => "html"46 req.accepts('text/html');47 // => "text/html"48 req.accepts('json, text');49 // => "json"50 req.accepts('application/json');51 // => "application/json"52 53 // Accept: text/*, application/json54 req.accepts('image/png');55 req.accepts('png');56 // => undefined57 58 // Accept: text/*;q=.5, application/json59 req.accepts(['html', 'json']);60 req.accepts('html, json');61 // => "json"6263# req.acceptsCharset()6465 Check if the given `charset` is acceptable,66 otherwise you should respond with 406 "Not Acceptable".6768# req.acceptsLanguage()6970 Check if the given `lang` is acceptable,71 otherwise you should respond with 406 "Not Acceptable".7273# req.param()7475 Return the value of param `name` when present or `defaultValue`.76 77 - Checks body params, ex: id=12, {"id":12}78 - Checks route placeholders, ex: _/user/:id_79 - Checks query string params, ex: ?id=1280 81 To utilize request bodies, `req.body`82 should be an object. This can be done by using83 the `connect.bodyParser()` middleware.8485# req.is()8687 Check if the incoming request contains the "Content-Type" 88 header field, and it contains the give mime `type`.89 90 ## Examples91 92 // With Content-Type: text/html; charset=utf-893 req.is('html');94 req.is('text/html');95 req.is('text/*');96 // => true97 98 // When Content-Type is application/json99 req.is('json');100 req.is('application/json');101 req.is('application/*');102 // => true103 104 req.is('html');105 // => false106 107 Now within our route callbacks, we can use to to assert content types108 such as "image/jpeg", "image/png", etc.109 110 app.post('/image/upload', function(req, res, next){111 if (req.is('image/*')) {112 // do something113 } else {114 next();115 }116 });117