response.md
This file
Path
docs/response.md
218 lines · 5.4 KiB · Markdown
Last changed by
update markdown docsTJ Holowaychukbefore adoption76aa718
History of this fileTake it
Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 1–75why, one step back12# res34 Response prototype.56# res.status()78 Set status `code`.910# res.send()1112 Send a response.13 14 ## Examples15 16 res.send(new Buffer('wahoo'));17 res.send({ some: 'json' });18 res.send('<p>some html</p>');19 res.send(404, 'Sorry, cant find that');20 res.send(404);2122# res.json()2324 Send JSON response.25 26 ## Examples27 28 res.json(null);29 res.json({ user: 'tj' });30 res.json(500, 'oh noes!');31 res.json(404, 'I dont have that');3233# res.sendfile()3435 Transfer the file at the given `path`.36 37 Automatically sets the _Content-Type_ response header field.38 The callback `fn(err)` is invoked when the transfer is complete39 or when an error occurs. Be sure to check `res.sentHeader`40 if you wish to attempt responding, as the header and some data41 may have already been transferred.42 43 ## Options44 45 - `maxAge` defaulting to 046 - `root` root directory for relative filenames47 48 ## Examples49 50 The following example illustrates how `res.sendfile()` may51 be used as an alternative for the `static()` middleware for52 dynamic situations. The code backing `res.sendfile()` is actually53 the same code, so HTTP cache support etc is identical.54 55 app.get('/user/:uid/photos/:file', function(req, res){56 var uid = req.params.uid57 , file = req.params.file;58 59 req.user.mayViewFilesFrom(uid, function(yes){60 if (yes) {61 res.sendfile('/uploads/' + uid + '/' + file);62 } else {63 res.send(403, 'Sorry! you cant see that.');64 }65 });66 });6768# res.download()6970 Transfer the file at the given `path` as an attachment.71 72 Optionally providing an alternate attachment `filename`,73 and optional callback `fn(err)`. The callback is invoked74 when the data transfer is complete, or when an error has75 ocurred. Be sure to check `res.headerSent` if you plan to respond.update markdown docsTJ Holowaychukbefore adoption76aa718
lines 76–77why, one step back76 77 This method uses `res.sendfile()`.Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 78–109why, one step back7879# res.format()8081 Respond to the Acceptable formats using an `obj`82 of mime-type callbacks.83 84 This method uses `req.accepted`, an array of85 acceptable types ordered by their quality values.86 When "Accept" is not present the _first_ callback87 is invoked, otherwise the first match is used. When88 no match is performed the server responds with89 406 "Not Acceptable".90 91 Content-Type is set for you, however if you choose92 you may alter this within the callback using `res.type()`93 or `res.set('Content-Type', ...)`.94 95 res.format({96 'text/plain': function(){97 res.send('hey');98 },99 100 'text/html': function(){101 res.send('<p>hey</p>');102 },103 104 'appliation/json': function(){105 res.send({ message: 'hey' });106 }107 });108 109 In addition to canonicalized MIME types you mayupdate markdown docsTJ Holowaychukbefore adoption76aa718
line 110why, one step back110 also use extnames mapped to these types:Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 111–122why, one step back111 112 res.format({113 text: function(){114 res.send('hey');115 },116 117 html: function(){118 res.send('<p>hey</p>');119 },120 121 json: function(){122 res.send({ message: 'hey' });update markdown docsTJ Holowaychukbefore adoption76aa718
line 123why, one step back123 }Added new dox API docsTJ Holowaychukbefore adoption7e81cad
line 124why, one step back124 });update markdown docsTJ Holowaychukbefore adoption76aa718
lines 125–130why, one step back125 126 By default Express passes an `Error`127 with a `.status` of 406 to `next(err)`128 if a match is not made, however you may129 provide an optional callback `fn` to130 be invoked instead.Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 131–135why, one step back131132# res.attachment()133134 Set _Content-Disposition_ header to _attachment_ with optional `filename`.135update markdown docsTJ Holowaychukbefore adoption76aa718
line 136why, one step back136# res.setAdded new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 137–138why, one step back137138 Set header `field` to `val`, or passupdate markdown docsTJ Holowaychukbefore adoption76aa718
line 139why, one step back139 an object of header fields.Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 140–144why, one step back140 141 ## Examples142 143 res.set('Accept', 'application/json');144 res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });update markdown docsTJ Holowaychukbefore adoption76aa718
lines 145–146why, one step back145 146 Aliased as `res.header()`.Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 147–162why, one step back147148# res.get()149150 Get value for header `field`.151152# res.clearCookie()153154 Clear cookie `name`.155156# res.cookie()157158 Set cookie `name` to `val`, with the given `options`.159 160 ## Options161 162 - `maxAge` max-age in milliseconds, converted to `expires`update markdown docsTJ Holowaychukbefore adoption76aa718
line 163why, one step back163 - `signed` sign the cookieAdded new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 164–218why, one step back164 - `path` defaults to "/"165 166 ## Examples167 168 // "Remember Me" for 15 minutes169 res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });170 171 // save as above172 res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })173174# res.redirect()175176 Redirect to the given `url` with optional response `status`177 defaulting to 302.178 179 The given `url` can also be the name of a mapped url, for180 example by default express supports "back" which redirects181 to the _Referrer_ or _Referer_ headers or "/".182 183 ## Examples184 185 res.redirect('/foo/bar');186 res.redirect('http://example.com');187 res.redirect(301, 'http://example.com');188 res.redirect('../login'); // /blog/post/1 -> /blog/login189 190 ## Mounting191 192 When an application is mounted, and `res.redirect()`193 is given a path that does _not_ lead with "/". For 194 example suppose a "blog" app is mounted at "/blog",195 the following redirect would result in "/blog/login":196 197 res.redirect('login');198 199 While the leading slash would result in a redirect to "/login":200 201 res.redirect('/login');202203# res.render()204205 Render `view` with the given `options` and optional callback `fn`.206 When a callback function is given a response will _not_ be made207 automatically, otherwise a response of _200_ and _text/html_ is given.208 209 ## Options210 211 - `status` Response status code (`res.statusCode`)212 - `charset` Set the charset (`res.charset`)213 214 ## Reserved locals215 216 - `cache` boolean hinting to the engine it should cache217 - `filename` filename of the view being rendered218