Merge branch 'feature/res-json'
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 · parents e4342a7, 6db19db · Browse files · 3 files·+75 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+75 added−0 removed
- tests:+39 added−0 removed
- other:+36 added−0 removed
Provenance
- Commit
9e4020e- Tree
80c0a23- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
e4342a7- Parent 2
6db19db
3 files changed+75 added−0 removed
modified:docs/guide.md+12 added
913 unmodified lines914 it will not be set again.915 916 Note that this method _end()_s the response, so you will want to use node's _res.write()_ for multiple writes or streaming.917 918+### res.json(obj[, headers|status[, status]])919+920+ Send a JSON response with optional _headers_ and _status_. This method921+ is ideal for JSON-only APIs, however _res.send(obj)_ will send JSON as922+ well, though not ideal for cases when you want to send for example a string923+ as JSON, since the default for _res.send(string)_ is text/html.924+925+ res.json(null);926+ res.json({ user: 'tj' });927+ res.json('oh noes!', 500);928+ res.json('I dont have that', 404);929+930 ### res.redirect(url[, status])931 932 Redirect to the given _url_ with a default response _status_ of 302.933 473 unmodified linesmodified:lib/response.js+24 added
112 unmodified lines113 114 // respond115 this.statusCode = status;116 this.end('HEAD' == this.req.method ? undefined : body);117+ return this;118 };119 120 /**121+ * Send JSON response with `obj`, optional `headers`, and optional `status`.122+ *123+ * Examples:124+ *125+ * res.json(null);126+ * res.json({ user: 'tj' });127+ * res.json('oh noes!', 500);128+ * res.json('I dont have that', 404);129+ *130+ * @param {Mixed} obj131+ * @param {Object|Number} headers or status132+ * @param {Number} status133+ * @return {ServerResponse}134+ * @api public135+ */136+137+res.json = function(obj, headers, status){138+ this.charset = this.charset || 'utf-8';139+ this.header('Content-Type', 'application/json');140+ return this.send(JSON.stringify(obj), headers, status);141+};142+143+/**144 * Transfer the file at the given `path`. Automatically sets 145 * the _Content-Type_ response header field. `next()` is called146 * when `path` is a directory, or when an error occurs.147 *299 unmodified linesmodified:test/response.test.js+39 added
7 unmodified lines8 , assert = require('assert')9 , should = require('should');10 11 module.exports = {12+ 'test #json()': function(){13+ var app = express.createServer()14+ , json = 'application/json; charset=utf-8';15+16+ app.get('/user', function(req, res, next){17+ res.json({ name: 'tj' });18+ });19+20+ app.get('/string', function(req, res, next){21+ res.json('whoop!');22+ });23+24+ app.get('/error', function(req, res, next){25+ res.json('oh noes!', 500);26+ });27+28+ app.get('/headers', function(req, res, next){29+ res.json(undefined, { 'X-Foo': 'bar' }, 302);30+ });31+32+ assert.response(app,33+ { url: '/error' },34+ { body: '"oh noes!"'35+ , status: 50036+ , headers: { 'Content-Type': json }});37+38+ assert.response(app,39+ { url: '/string' },40+ { body: '"whoop!"'41+ , headers: {42+ 'Content-Type': json43+ , 'Content-Length': 844+ }});45+46+ assert.response(app,47+ { url: '/user' },48+ { body: '{"name":"tj"}', headers: { 'Content-Type': json }});49+ },50+51 'test #send()': function(){52 var app = express.createServer();53 54 app.get('/html', function(req, res){606 unmodified lines