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
Provenance
- Commit
c35cd7f- Tree
ddf73d5- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
bb9d505
4 files changed+65 added−47 removed
modified:docs/guide.md+27 added-16 removed
243 unmodified lines244 res.send('updating ' + req.user.name);245 });246 247 app.get('*', function(req, res){248- res.send('what???', 404);248+ res.send(404, 'what???');249 });250 251 app.listen(3000); 252 175 unmodified lines428 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 });434 435 ### Route Param Pre-conditions436 359 unmodified lines796 Sets the _Content-Disposition_ response header to "attachment", with optional _filename_.797 798 res.attachment('path/to/my/image.png');799 800+### res.status(code)801+802+ Sets the `res.statusCode` property to `code` and returns for chaining:803+804+ res.status(500).send('Something bad happened');805+806+ is equivalent to:807+ 808+ res.statusCode = 500;809+ res.send('Something bad happened');810+811+ and:812+813+ res.send(500, 'Something bad happened');814+815 ### res.sendfile(path[, options[, callback]])816 817 Used by `res.download()` to transfer an arbitrary file. 818 41 unmodified lines860 }, function(err){861 // connection related error862 });863 849-### res.send(body|status[, headers|status[, status]])864+### res.send(body|status[, body])865 866 The _res.send()_ method is a high level response utility allowing you to pass867 objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:868 869 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"876 861-By default the _Content-Type_ response header is set, however if explicitly862-assigned through `res.send()` or previously with `res.header()` or `res.contentType()`863-it will not be set again.877+The _Content-Type_ response header is defaulted appropriately unless previously defined via `res.header()` / `res.contentType()` etc.878 879 Note that this method _end()_s the response, so you will want to use node's _res.write()_ for multiple writes or streaming.880 867-### res.json(obj[, headers|status[, status]])881+### res.json(obj|status[, obj])882 869- 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.884 885 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');889 890 ### res.redirect(url[, status])891 892 Redirect to the given _url_ with a default response _status_ of 302.472 unmodified linesmodified:examples/web-service/app.js+2 added-2 removed
44 unmodified lines45464748494950515253545556565758596061 unmodified lines44 unmodified linesapp.use(function(err, req, res, next){ // whatever you want here, feel free to populate // properties on `err` to treat it differently in here, // or when you next(err) set res.statusCode= etc. res.send({ error: err.message }, 500); res.send(500, { error: err.message });});
// our custom JSON 404 middleware. Since it's placed last// it will be the last middleware called, if all others// invoke next() and do not respond.app.use(function(req, res){ res.send({ error: "Lame, can't find that" }, 404); res.send(404, { error: "Lame, can't find that" });});
/** * Generate our unique identifier.61 unmodified linesmodified:lib/response.js+30 added-23 removed
21 unmodified lines22 , basename = path.basename23 , join = path.join;24 25 /**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 */41 42-res.send = function(body, status){43- // default status44- status = status || this.statusCode;42+res.send = function(body){43+ // allow status / body44+ if (2 == arguments.length) {45+ this.statusCode = body;46+ body = arguments[1];47+ }48 46- // 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 lines64 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 lines79 : Buffer.byteLength(body));80 }81 82 // 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 }88 89 // respond87- this.statusCode = status;90 this.end('HEAD' == this.req.method ? undefined : body);91 return this;92 };93 94 /**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 */109 109-res.json = function(obj, status){110+res.json = function(obj){111+ // allow status / body112+ if (2 == arguments.length) {113+ this.statusCode = obj;114+ obj = arguments[1];115+ }116+117 var body = JSON.stringify(obj)118 , callback = this.req.query.callback119 , jsonp = this.app.enabled('jsonp callback');120 4 unmodified lines125 this.header('Content-Type', 'text/javascript');126 body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';127 }128 122- return this.send(body, status);129+ return this.send(body);130 };131 132 /**133 * Set status `code`.278 unmodified linesmodified:test/response.test.js+6 added-6 removed
20 unmodified lines21 res.json('whoop!');22 });23 24 app.get('/error', function(req, res, next){25- res.json('oh noes!', 500);25+ res.json(500, 'oh noes!');26 });27 28 assert.response(app,29 { url: '/error' },34 unmodified lines64 });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 lines77 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 lines193 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 lines230 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