Added response helpers
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 f0bebd4 · Browse files · 3 files·+94 added−1 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+94 added−1 removed
- tests:+27 added−0 removed
- other:+67 added−1 removed
Provenance
- Commit
874975d- Tree
0eea02a- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
f0bebd4
3 files changed+94 added−1 removed
modified:lib/express/index.js+2 added-1 removed
31 unmodified lines3233343536363731 unmodified lines/** * View extensions. */
require('./view');No newline at end of filerequire('./view');require('./response');No newline at end of fileadded:lib/express/response.js+65 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
/*! * Express - http * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca> * MIT Licensed */
/** * Module dependencies. */
var http = require('http'), path = require('path'), mime = require('connect/utils').mime, Buffer = require('buffer').Buffer;
/** * Send a response with the given `body` and optional `headers` and `status` code. * * Currently supports: * * - html * - json * * @param {String|Object} body * @param {Object} headers * @param {Number} status * @return {ServerResponse} * @api public */
http.ServerResponse.prototype.send = function(body, headers, status){ status = status || 200; headers = headers || {}; switch (typeof body) { case 'string': headers['Content-Type'] = 'text/html; charset=utf8'; break; case 'object': headers['Content-Type'] = 'application/json; charset=utf8'; body = JSON.stringify(body); break; } headers['Content-Length'] = Buffer.byteLength(body); this.writeHead(status, headers); this.end(body);};
/** * Set _Content-Type_ response header passed through `mime.type()`. * * Examples: * * var filename = 'path/to/image.png'; * res.contentType(filename); * // => "image/png" * * @param {String} type * @return {String} the resolved mime type * @api public */
http.ServerResponse.prototype.contentType = function(type){ return this.headers['Content-Type'] = mime.type(type);};added:test/response.test.js+27 added
123456789101112131415161718192021222324252627
/** * Module dependencies. */
var express = require('express');
module.exports = { '#send()': function(assert){ var app = express.createServer();
app.get('/html', function(req, res){ res.send('<p>test</p>', { 'Content-Language': 'en' }); }); app.get('/json', function(req, res){ res.send({ foo: 'bar' }, {}, 201); });
assert.response(app, { url: '/html' }, { body: '<p>test</p>', headers: { 'Content-Language': 'en', 'Content-Type': 'text/html; charset=utf8' }}); assert.response(app, { url: '/json' }, { body: '{"foo":"bar"}', status: 201, headers: { 'Content-Type': 'application/json; charset=utf8' }}); }};No newline at end of file