Skip to content
Myr

response.md

This file

Path

docs/response.md

212 lines · 5.2 KiB · Markdown

Last changed by

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

History of this file

Take it

Added new dox API docsTJ Holowaychukbefore adoption7e81cad

lines 1–212why, one step back
1
2# 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.7677# res.format()7879 Respond to the Acceptable formats using an `obj`80 of mime-type callbacks.81 82 This method uses `req.accepted`, an array of83 acceptable types ordered by their quality values.84 When "Accept" is not present the _first_ callback85 is invoked, otherwise the first match is used. When86 no match is performed the server responds with87 406 "Not Acceptable".88 89 Content-Type is set for you, however if you choose90 you may alter this within the callback using `res.type()`91 or `res.set('Content-Type', ...)`.92 93 res.format({94 'text/plain': function(){95 res.send('hey');96 },97 98 'text/html': function(){99 res.send('<p>hey</p>');100 },101 102 'appliation/json': function(){103 res.send({ message: 'hey' });104 }105 });106 107 In addition to canonicalized MIME types you may108 ## also use extnames mapped to these types109 110 res.format({111 text: function(){112 res.send('hey');113 },114 115 html: function(){116 res.send('<p>hey</p>');117 },118 119 json: function(){120 res.send({ message: 'hey' });121 }122 });123124# res.attachment()125126 Set _Content-Disposition_ header to _attachment_ with optional `filename`.127128# res.set()129130 Set header `field` to `val`, or pass131 an object of of header fields.132 133 ## Examples134 135 res.set('Accept', 'application/json');136 res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });137138# res.get()139140 Get value for header `field`.141142# res.clearCookie()143144 Clear cookie `name`.145146# res.signedCookie()147148 Set a signed cookie with the given `name` and `val`.149 See `res.cookie()` for details.150151# res.cookie()152153 Set cookie `name` to `val`, with the given `options`.154 155 ## Options156 157 - `maxAge` max-age in milliseconds, converted to `expires`158 - `path` defaults to "/"159 160 ## Examples161 162 // "Remember Me" for 15 minutes163 res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });164 165 // save as above166 res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })167168# res.redirect()169170 Redirect to the given `url` with optional response `status`171 defaulting to 302.172 173 The given `url` can also be the name of a mapped url, for174 example by default express supports "back" which redirects175 to the _Referrer_ or _Referer_ headers or "/".176 177 ## Examples178 179 res.redirect('/foo/bar');180 res.redirect('http://example.com');181 res.redirect(301, 'http://example.com');182 res.redirect('../login'); // /blog/post/1 -> /blog/login183 184 ## Mounting185 186 When an application is mounted, and `res.redirect()`187 is given a path that does _not_ lead with "/". For 188 example suppose a "blog" app is mounted at "/blog",189 the following redirect would result in "/blog/login":190 191 res.redirect('login');192 193 While the leading slash would result in a redirect to "/login":194 195 res.redirect('/login');196197# res.render()198199 Render `view` with the given `options` and optional callback `fn`.200 When a callback function is given a response will _not_ be made201 automatically, otherwise a response of _200_ and _text/html_ is given.202 203 ## Options204 205 - `status` Response status code (`res.statusCode`)206 - `charset` Set the charset (`res.charset`)207 208 ## Reserved locals209 210 - `cache` boolean hinting to the engine it should cache211 - `filename` filename of the view being rendered212