Skip to content
Myr

Changed `res.{send,json}()` status code to the first arg at all times

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent bb9d505 · Browse files · 4 files+65 added−47 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+65 added−47 removed

  • tests:+6 added−6 removed
  • other:+59 added−41 removed

4 files+65 added−47 removed

modified:docs/guide.md+27 added-16 removed
243 unmodified lines
244 res.send('updating ' + req.user.name);245 });246247 app.get('*', function(req, res){248 res.send('what???', 404);248 res.send(404, 'what???');249 });250251 app.listen(3000); 252
175 unmodified lines
428 app.use(app.bodyParser());429 app.use(app.methodOverride());430 app.use(app.router);431 app.use(function(err, req, res, next){432 res.send('Internal Server Error', 500);432 res.send(500, 'Server error');433 });434435### Route Param Pre-conditions436
359 unmodified lines
796Sets the _Content-Disposition_ response header to "attachment", with optional _filename_.797798 res.attachment('path/to/my/image.png');799800### res.status(code)801802 Sets the `res.statusCode` property to `code` and returns for chaining:803804 res.status(500).send('Something bad happened');805806 is equivalent to:807 808 res.statusCode = 500;809 res.send('Something bad happened');810811 and:812813 res.send(500, 'Something bad happened');814815### res.sendfile(path[, options[, callback]])816817Used by `res.download()` to transfer an arbitrary file. 818
41 unmodified lines
860 }, function(err){861 // connection related error862 });863849### res.send(body|status[, headers|status[, status]])864### res.send(body|status[, body])865866The _res.send()_ method is a high level response utility allowing you to pass867objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:868869 res.send(new Buffer('wahoo'));870 res.send({ some: 'json' });871 res.send(201, { message: 'User created' });872 res.send('<p>some html</p>');857 res.send('Sorry, cant find that', 404);858 res.send('text', { 'Content-Type': 'text/plain' }, 201);859 res.send(404);873 res.send(404, 'Sorry, cant find that');874 res.send(404); // "Not Found"875 res.send(500); // "Internal Server Error"876861By default the _Content-Type_ response header is set, however if explicitly862assigned through `res.send()` or previously with `res.header()` or `res.contentType()`863it will not be set again.877The _Content-Type_ response header is defaulted appropriately unless previously defined via `res.header()` / `res.contentType()` etc.878879Note that this method _end()_s the response, so you will want to use node's _res.write()_ for multiple writes or streaming.880867### res.json(obj[, headers|status[, status]])881### res.json(obj|status[, obj])882869 Send a JSON response with optional _headers_ and _status_. This method870 is ideal for JSON-only APIs, however _res.send(obj)_ will send JSON as871 well, though not ideal for cases when you want to send for example a string872 as JSON, since the default for _res.send(string)_ is text/html.883 Send an explicit JSON response. This method is ideal for JSON-only APIs, while it is much like _res.send(obj)_, send is not ideal for cases when you want to send for example a single string as JSON, since the default for _res.send(string)_ is text/html.884885 res.json(null);886 res.json({ user: 'tj' });876 res.json('oh noes!', 500);877 res.json('I dont have that', 404);887 res.json(500, 'oh noes!');888 res.json(404, 'I dont have that');889890### res.redirect(url[, status])891892Redirect to the given _url_ with a default response _status_ of 302.
472 unmodified lines
modified:examples/web-service/app.js+2 added-2 removed
modified:lib/response.js+30 added-23 removed
21 unmodified lines
22 , basename = path.basename23 , join = path.join;2425/**26 * Send a response with the given `body` and optional `status` code.26 * Send a response.27 *28 * Examples:29 *30 * res.send(new Buffer('wahoo'));31 * res.send({ some: 'json' });32 * res.send('<p>some html</p>');33 * res.send('Sorry, cant find that', 404);33 * res.send(404, 'Sorry, cant find that');34 * res.send(404);35 *36 * @param {String|Object|Number|Buffer} body or status37 * @param {Number} status36 * @param {Mixed} body or status37 * @param {Mixed} body38 * @return {ServerResponse}39 * @api public40 */4142res.send = function(body, status){43 // default status44 status = status || this.statusCode;42res.send = function(body){43 // allow status / body44 if (2 == arguments.length) {45 this.statusCode = body;46 body = arguments[1];47 }4846 // determine content type49 switch (typeof body) {50 // response status51 case 'number':49 if (!this.header('Content-Type')) {50 this.contentType('.txt');51 }52 body = http.STATUS_CODES[status = body];52 this.header('Content-Type') || this.contentType('.txt');53 this.statusCode = body;54 body = http.STATUS_CODES[body];55 break;56 // string defaulting to html57 case 'string':58 if (!this.header('Content-Type')) {59 this.charset = this.charset || 'utf-8';60 this.contentType('.html');
3 unmodified lines
64 case 'object':65 if (null == body) {66 body = '';67 } else if (Buffer.isBuffer(body)) {65 if (!this.header('Content-Type')) this.contentType('.bin');68 this.header('Content-Type') || this.contentType('.bin');69 } else {67 return this.json(body, status);70 return this.json(body);71 }72 break;73 }74
4 unmodified lines
79 : Buffer.byteLength(body));80 }8182 // strip irrelevant headers80 if (204 == status || 304 == status) {83 if (204 == this.statusCode || 304 == this.statusCode) {84 this.removeHeader('Content-Type');85 this.removeHeader('Content-Length');86 body = '';87 }8889 // respond87 this.statusCode = status;90 this.end('HEAD' == this.req.method ? undefined : body);91 return this;92};9394/**93 * Send JSON response with `obj` and optional `status`.95 * Send JSON response.96 *97 * Examples:98 *99 * res.json(null);100 * res.json({ user: 'tj' });99 * res.json('oh noes!', 500);100 * res.json('I dont have that', 404);101 * res.json(500, 'oh noes!');102 * res.json(404, 'I dont have that');103 *104 * @param {Mixed} obj or status105 * @param {Mixed} obj103 * @param {Number} status104 * @param {Number} status106 * @return {ServerResponse}107 * @api public108 */109109res.json = function(obj, status){110res.json = function(obj){111 // allow status / body112 if (2 == arguments.length) {113 this.statusCode = obj;114 obj = arguments[1];115 }116117 var body = JSON.stringify(obj)118 , callback = this.req.query.callback119 , jsonp = this.app.enabled('jsonp callback');120
4 unmodified lines
125 this.header('Content-Type', 'text/javascript');126 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';127 }128122 return this.send(body, status);129 return this.send(body);130};131132/**133 * Set status `code`.
278 unmodified lines
modified:test/response.test.js+6 added-6 removed
20 unmodified lines
21 res.json('whoop!');22 });2324 app.get('/error', function(req, res, next){25 res.json('oh noes!', 500);25 res.json(500, 'oh noes!');26 });2728 assert.response(app,29 { url: '/error' },
34 unmodified lines
64 });65 66 app.get('/json', function(req, res){67 res.header('X-Foo', 'bar');68 res.send({ foo: 'bar' }, 201);68 res.send(201, { foo: 'bar' });69 });70 71 app.get('/text', function(req, res){72 res.header('X-Foo', 'bar').contentType('txt');
4 unmodified lines
77 res.send(404);78 });79 80 app.get('/status/text', function(req, res){81 res.send('Oh noes!', 404);81 res.send(404, 'Oh noes!');82 });83 84 app.get('/error', function(req, res){85 res.header('Content-Type', 'text/plain');86 res.send('Oh shit!', 500);86 res.send(500, 'Oh shit!');87 });88 89 app.get('/buffer', function(req, res){90 res.send(new Buffer('wahoo!'));
102 unmodified lines
193 app.enable('jsonp callback');194 195 app.get('/jsonp', function(req, res){196 res.header('X-Foo', 'bar');197 res.send({ foo: 'bar' }, 201);197 res.send(201, { foo: 'bar' });198 });199 200 assert.response(app,201 { url: '/jsonp?callback=test' },
28 unmodified lines
230 app.enable('jsonp callback');231 232 app.get('/jsonp', function(req, res){233 res.header('X-Foo', 'bar');234 res.json({ foo: 'bar' }, 201);234 res.json(201, { foo: 'bar' });235 });236 237 assert.response(app,238 { url: '/jsonp?callback=test' },
535 unmodified lines