Skip to content
Myr

Replace res.send() with res.send = require("response-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 2173d00 · Browse files · 2 files+2 added−77 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+2 added−77 removed

  • other:+2 added−77 removed

2 files+2 added−77 removed

modified:lib/response.js+1 added-76 removed
11 unmodified lines
12 , normalizeTypes = require('./utils').normalizeTypes13 , statusCodes = http.STATUS_CODES14 , send = connect.static.send15 , cookie = require('cookie')16 , crc = require('crc')16 , mime = connect.mime17 , basename = path.basename18 , extname = path.extname19 , join = path.join;
56 unmodified lines
76 * @return {ServerResponse}77 * @api public78 */7981res.send = function(body){82 var req = this.req83 , head = 'HEAD' == req.method84 , len;80res.send = require('response-send');8186 // allow status / body87 if (2 == arguments.length) {88 // res.send(body, status) backwards compat89 if ('number' != typeof body && 'number' == typeof arguments[1]) {90 this.statusCode = arguments[1];91 } else {92 this.statusCode = body;93 body = arguments[1];94 }95 }969798 // convert string objects to primitives99 if (body instanceof String) body = body.toString();100101 switch (typeof body) {102 // response status103 case 'number':104 this.get('Content-Type') || this.type('txt');105 this.statusCode = body;106 body = http.STATUS_CODES[body];107 break;108 // string defaulting to html109 case 'string':110 if (!this.get('Content-Type')) {111 this.charset = this.charset || 'utf-8';112 this.type('html');113 }114 break;115 case 'boolean':116 case 'object':117 if (null == body) {118 body = '';119 } else if (Buffer.isBuffer(body)) {120 this.get('Content-Type') || this.type('bin');121 } else {122 return this.json(body);123 }124 break;125 }126127 // populate Content-Length128 if (undefined !== body && !this.get('Content-Length')) {129 this.set('Content-Length', len = Buffer.isBuffer(body)130 ? body.length131 : Buffer.byteLength(body));132 }133134 // ETag support135 // TODO: W/ support136 if (len > 1024) {137 if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)138 ? crc.buffer.crc32(body)139 : crc.crc32(body));140 }141142 // freshness143 if (req.fresh) this.statusCode = 304;144145 // strip irrelevant headers146 if (204 == this.statusCode || 304 == this.statusCode) {147 this.removeHeader('Content-Type');148 this.removeHeader('Content-Length');149 body = '';150 }151152 // respond153 this.end(head ? null : body);154 return this;155};15682/**83 * Send JSON response.84 *85 * Examples:
529 unmodified lines
modified:package.json+1 added-1 removed