Added buffer support to res.send()
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 8fdf111 · Browse files · 2 files·+19 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+19 added−11 removed
- tests:+9 added−1 removed
- other:+10 added−10 removed
Provenance
- Commit
14280df- Tree
0285d94- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
8fdf111
2 files changed+19 added−11 removed
modified:lib/express/response.js+10 added-10 removed
16 unmodified lines17181920212223242521222324252627282934303132333423 unmodified lines585960616266676364656667696869707172757374757677787953 unmodified lines16 unmodified lines
/** * Send a response with the given `body` and optional `headers` and `status` code. * * Currently supports: * * - html * - json * * Examples: * * res.send(new Buffer('wahoo')); * res.send({ some: 'json' }); * res.send('<p>some html</p>'); * res.send('Sorry, cant find that', 404); * res.send('text', { 'Content-Type': 'text/plain' }, 201); * res.send(404); * * @param {String|Object|Number} body or status * @param {String|Object|Number|Buffer} body or status * @param {Object|Number} headers or status * @param {Number} status * @return {ServerResponse} * @api public23 unmodified lines this.contentType('.html'); } break; case 'object': if (!(body instanceof Buffer)) { if (!this.headers['Content-Type']) { this.contentType('.json'); if (!this.headers['Content-Type']) { this.contentType('.json'); } body = JSON.stringify(body); } body = JSON.stringify(body); break; }
// Populate Content-Length if (!this.headers['Content-Length']) { this.header('Content-Length', Buffer.byteLength(body)); this.header('Content-Length', body instanceof Buffer ? body.length : Buffer.byteLength(body)); }
// Merge headers passed utils.merge(this.headers, headers);53 unmodified linesmodified:test/response.test.js+9 added-1 removed
1 unmodified line2 /**3 * Module dependencies.4 */5 6-var express = require('express');6+var express = require('express'),7+ Buffer = require('buffer').Buffer;8 9 module.exports = {10 '#send()': function(assert){11 var app = express.createServer();19 unmodified lines31 32 app.get('/error', function(req, res){33 res.send('Oh shit!', { 'Content-Type': 'text/plain' }, 500);34 });35+ 36+ app.get('/buffer', function(req, res){37+ res.send(new Buffer('wahoo!'));38+ });39 40 assert.response(app,41 { url: '/html' },42 { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html; charset=utf-8' }});8 unmodified lines51 { body: 'Not Found', status: 404, headers: { 'Content-Type': 'text/plain; charset=utf-8' }});52 assert.response(app,53 { url: '/error' },54 { body: 'Oh shit!', status: 500, headers: { 'Content-Type': 'text/plain' }});55+ assert.response(app,56+ { url: '/buffer' },57+ { body: 'wahoo!' });58 },59 60 '#contentType': function(assert){61 var app = express.createServer();33 unmodified lines