5 var contentDisposition = require('content-disposition');
6 var deprecate = require('depd')('express');
7 var escapeHtml = require('escape-html');
8 var http = require('http');
9 var isAbsolute = require('./utils').isAbsolute;
10 var onFinished = require('on-finished');
11 var path = require('path');
12 var merge = require('utils-merge');
13 var sign = require('cookie-signature').sign;
14 var normalizeType = require('./utils').normalizeType;
15 var normalizeTypes = require('./utils').normalizeTypes;
16 var setCharset = require('./utils').setCharset;
17 var statusCodes = http.STATUS_CODES;
18 var cookie = require('cookie');
19 var send = require('send');
20 var extname = path.extname;
22 var resolve = path.resolve;
23 var vary = require('vary');
29 var res = module.exports = {
30 __proto__: http.ServerResponse.prototype
36 * @param {Number} code
37 * @return {ServerResponse}
41 res.status = function(code){
42 this.statusCode = code;
47 * Set Link header field with the given `links`.
52 * next: 'http://api.example.com/users?page=2',
53 * last: 'http://api.example.com/users?page=5'
56 * @param {Object} links
57 * @return {ServerResponse}
61 res.links = function(links){
62 var link = this.get('Link') || '';
63 if (link) link += ', ';
64 return this.set('Link', link + Object.keys(links).map(function(rel){
65 return '<' + links[rel] + '>; rel="' + rel + '"';
74 * res.send(new Buffer('wahoo'));
75 * res.send({ some: 'json' });
76 * res.send('<p>some html</p>');
78 * @param {string|number|boolean|object|Buffer} body
82 res.send = function send(body) {
92 // allow status / body
93 if (arguments.length === 2) {
94 // res.send(body, status) backwards compat
95 if (typeof arguments[0] !== 'number' && typeof arguments[1] === 'number') {
96 deprecate('res.send(body, status): Use res.status(status).send(body) instead');
97 this.statusCode = arguments[1];
99 deprecate('res.send(status, body): Use res.status(status).send(body) instead');
100 this.statusCode = arguments[0];
101 chunk = arguments[1];
105 // disambiguate res.send(status) and res.send(status, num)
106 if (typeof chunk === 'number' && arguments.length === 1) {
107 // res.send(status) will set status message as text string
108 if (!this.get('Content-Type')) {
112 deprecate('res.send(status): Use res.sendStatus(status) instead');
113 this.statusCode = chunk;
114 chunk = http.STATUS_CODES[chunk];
117 switch (typeof chunk) {
118 // string defaulting to html
120 if (!this.get('Content-Type')) {
127 if (chunk === null) {
129 } else if (Buffer.isBuffer(chunk)) {
130 if (!this.get('Content-Type')) {
134 return this.json(chunk);
139 // write strings in utf-8
140 if (typeof chunk === 'string') {
142 type = this.get('Content-Type');
144 // reflect this in content-type
145 if (typeof type === 'string') {
146 this.set('Content-Type', setCharset(type, 'utf-8'));
150 // populate Content-Length
151 if (chunk !== undefined) {
152 if (!Buffer.isBuffer(chunk)) {
153 // convert chunk to Buffer; saves later double conversions
154 chunk = new Buffer(chunk, encoding);
155 encoding = undefined;
159 this.set('Content-Length', len);
163 var isHead = req.method === 'HEAD';
166 if (len !== undefined && (isHead || req.method === 'GET')) {
167 var etag = app.get('etag fn');
168 if (etag && !this.get('ETag')) {
169 etag = etag(chunk, encoding);
170 etag && this.set('ETag', etag);
175 if (req.fresh) this.statusCode = 304;
177 // strip irrelevant headers
178 if (204 == this.statusCode || 304 == this.statusCode) {
179 this.removeHeader('Content-Type');
180 this.removeHeader('Content-Length');
181 this.removeHeader('Transfer-Encoding');
186 // skip body for HEAD
190 this.end(chunk, encoding);
197 * Send JSON response.
202 * res.json({ user: 'tj' });
204 * @param {string|number|boolean|object} obj
208 res.json = function json(obj) {
211 // allow status / body
212 if (arguments.length === 2) {
213 // res.json(body, status) backwards compat
214 if (typeof arguments[1] === 'number') {
215 deprecate('res.json(obj, status): Use res.status(status).json(obj) instead');
216 this.statusCode = arguments[1];
218 deprecate('res.json(status, obj): Use res.status(status).json(obj) instead');
219 this.statusCode = arguments[0];
226 var replacer = app.get('json replacer');
227 var spaces = app.get('json spaces');
228 var body = JSON.stringify(val, replacer, spaces);
231 if (!this.get('Content-Type')) {
232 this.set('Content-Type', 'application/json');
235 return this.send(body);
239 * Send JSON response with JSONP callback support.
244 * res.jsonp({ user: 'tj' });
246 * @param {string|number|boolean|object} obj
250 res.jsonp = function jsonp(obj) {
253 // allow status / body
254 if (arguments.length === 2) {
255 // res.json(body, status) backwards compat
256 if (typeof arguments[1] === 'number') {
257 deprecate('res.jsonp(obj, status): Use res.status(status).json(obj) instead');
258 this.statusCode = arguments[1];
260 deprecate('res.jsonp(status, obj): Use res.status(status).jsonp(obj) instead');
261 this.statusCode = arguments[0];
268 var replacer = app.get('json replacer');
269 var spaces = app.get('json spaces');
270 var body = JSON.stringify(val, replacer, spaces);
271 var callback = this.req.query[app.get('jsonp callback name')];
274 if (!this.get('Content-Type')) {
275 this.set('X-Content-Type-Options', 'nosniff');
276 this.set('Content-Type', 'application/json');
280 if (Array.isArray(callback)) {
281 callback = callback[0];
285 if (typeof callback === 'string' && callback.length !== 0) {
286 this.charset = 'utf-8';
287 this.set('X-Content-Type-Options', 'nosniff');
288 this.set('Content-Type', 'text/javascript');
290 // restrict callback charset
291 callback = callback.replace(/[^\[\]\w$.]/g, '');
293 // replace chars not allowed in JavaScript that are in JSON
295 .replace(/\u2028/g, '\\u2028')
296 .replace(/\u2029/g, '\\u2029');
298 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"
299 // the typeof check is just to reduce client error noise
300 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';
303 return this.send(body);
307 * Send given HTTP status code.
309 * Sets the response status to `statusCode` and the body of the
310 * response to the standard description from node's http.STATUS_CODES
311 * or the statusCode number if no description.
315 * res.sendStatus(200);
317 * @param {number} statusCode
321 res.sendStatus = function sendStatus(statusCode) {
322 var body = http.STATUS_CODES[statusCode] || String(statusCode);
324 this.statusCode = statusCode;
327 return this.send(body);
331 * Transfer the file at the given `path`.
333 * Automatically sets the _Content-Type_ response header field.
334 * The callback `fn(err)` is invoked when the transfer is complete
335 * or when an error occurs. Be sure to check `res.sentHeader`
336 * if you wish to attempt responding, as the header and some data
337 * may have already been transferred.
341 * - `maxAge` defaulting to 0 (can be string converted by `ms`)
342 * - `root` root directory for relative filenames
343 * - `headers` object of headers to serve with file
344 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them
346 * Other options are passed along to `send`.
350 * The following example illustrates how `res.sendFile()` may
351 * be used as an alternative for the `static()` middleware for
352 * dynamic situations. The code backing `res.sendFile()` is actually
353 * the same code, so HTTP cache support etc is identical.
355 * app.get('/user/:uid/photos/:file', function(req, res){
356 * var uid = req.params.uid
357 * , file = req.params.file;
359 * req.user.mayViewFilesFrom(uid, function(yes){
361 * res.sendFile('/uploads/' + uid + '/' + file);
363 * res.send(403, 'Sorry! you cant see that.');
371 res.sendFile = function sendFile(path, options, fn) {
377 throw new TypeError('path argument is required to res.sendFile');
380 // support function as second arg
381 if (typeof options === 'function') {
386 options = options || {};
388 if (!options.root && !isAbsolute(path)) {
389 throw new TypeError('path must be absolute or specify root to res.sendFile');
392 // create file stream
393 var pathname = encodeURI(path);
394 var file = send(req, pathname, options);
397 sendfile(res, file, options, function (err) {
398 if (fn) return fn(err);
399 if (err && err.code === 'EISDIR') return next();
401 // next() all but write errors
402 if (err && err.code !== 'ECONNABORT' && err.syscall !== 'write') {
409 * Transfer the file at the given `path`.
411 * Automatically sets the _Content-Type_ response header field.
412 * The callback `fn(err)` is invoked when the transfer is complete
413 * or when an error occurs. Be sure to check `res.sentHeader`
414 * if you wish to attempt responding, as the header and some data
415 * may have already been transferred.
419 * - `maxAge` defaulting to 0 (can be string converted by `ms`)
420 * - `root` root directory for relative filenames
421 * - `headers` object of headers to serve with file
422 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them
424 * Other options are passed along to `send`.
428 * The following example illustrates how `res.sendfile()` may
429 * be used as an alternative for the `static()` middleware for
430 * dynamic situations. The code backing `res.sendfile()` is actually
431 * the same code, so HTTP cache support etc is identical.
433 * app.get('/user/:uid/photos/:file', function(req, res){
434 * var uid = req.params.uid
435 * , file = req.params.file;
437 * req.user.mayViewFilesFrom(uid, function(yes){
439 * res.sendfile('/uploads/' + uid + '/' + file);
441 * res.send(403, 'Sorry! you cant see that.');
449 res.sendfile = function(path, options, fn){
454 // support function as second arg
455 if (typeof options === 'function') {
460 options = options || {};
462 // create file stream
463 var file = send(req, path, options);
466 sendfile(res, file, options, function (err) {
467 if (fn) return fn(err);
468 if (err && err.code === 'EISDIR') return next();
470 // next() all but write errors
471 if (err && err.code !== 'ECONNABORT' && err.syscall !== 'write') {
477 res.sendfile = deprecate.function(res.sendfile,
478 'res.sendfile: Use res.sendFile instead');
481 * Transfer the file at the given `path` as an attachment.
483 * Optionally providing an alternate attachment `filename`,
484 * and optional callback `fn(err)`. The callback is invoked
485 * when the data transfer is complete, or when an error has
486 * ocurred. Be sure to check `res.headersSent` if you plan to respond.
488 * This method uses `res.sendfile()`.
493 res.download = function download(path, filename, fn) {
494 // support function as second arg
495 if (typeof filename === 'function') {
500 filename = filename || path;
502 // set Content-Disposition when file is sent
504 'Content-Disposition': contentDisposition(filename)
507 // Resolve the full path for sendFile
508 var fullPath = resolve(path);
510 return this.sendFile(fullPath, { headers: headers }, fn);
514 * Set _Content-Type_ response header with `type` through `mime.lookup()`
515 * when it does not contain "/", or set the Content-Type to `type` otherwise.
522 * res.type('application/json');
525 * @param {String} type
526 * @return {ServerResponse} for chaining
531 res.type = function(type){
532 return this.set('Content-Type', ~type.indexOf('/')
534 : mime.lookup(type));
538 * Respond to the Acceptable formats using an `obj`
539 * of mime-type callbacks.
541 * This method uses `req.accepted`, an array of
542 * acceptable types ordered by their quality values.
543 * When "Accept" is not present the _first_ callback
544 * is invoked, otherwise the first match is used. When
545 * no match is performed the server responds with
546 * 406 "Not Acceptable".
548 * Content-Type is set for you, however if you choose
549 * you may alter this within the callback using `res.type()`
550 * or `res.set('Content-Type', ...)`.
553 * 'text/plain': function(){
557 * 'text/html': function(){
558 * res.send('<p>hey</p>');
561 * 'appliation/json': function(){
562 * res.send({ message: 'hey' });
566 * In addition to canonicalized MIME types you may
567 * also use extnames mapped to these types:
575 * res.send('<p>hey</p>');
579 * res.send({ message: 'hey' });
583 * By default Express passes an `Error`
584 * with a `.status` of 406 to `next(err)`
585 * if a match is not made. If you provide
586 * a `.default` callback it will be invoked
589 * @param {Object} obj
590 * @return {ServerResponse} for chaining
594 res.format = function(obj){
598 var fn = obj.default;
599 if (fn) delete obj.default;
600 var keys = Object.keys(obj);
602 var key = req.accepts(keys);
607 this.set('Content-Type', normalizeType(key).value);
608 obj[key](req, this, next);
612 var err = new Error('Not Acceptable');
614 err.types = normalizeTypes(keys).map(function(o){ return o.value });
622 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.
624 * @param {String} filename
625 * @return {ServerResponse}
629 res.attachment = function attachment(filename) {
631 this.type(extname(filename));
634 this.set('Content-Disposition', contentDisposition(filename));
640 * Append additional header `field` with value `val`.
644 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
645 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
646 * res.append('Warning', '199 Miscellaneous warning');
648 * @param {String} field
649 * @param {String|Array} val
650 * @return {ServerResponse} for chaining
654 res.append = function append(field, val) {
655 var prev = this.get(field);
659 // concat the new and prev vals
660 value = Array.isArray(prev) ? prev.concat(val)
661 : Array.isArray(val) ? [prev].concat(val)
665 return this.set(field, value);
669 * Set header `field` to `val`, or pass
670 * an object of header fields.
674 * res.set('Foo', ['bar', 'baz']);
675 * res.set('Accept', 'application/json');
676 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
678 * Aliased as `res.header()`.
680 * @param {String|Object|Array} field
681 * @param {String} val
682 * @return {ServerResponse} for chaining
687 res.header = function header(field, val) {
688 if (arguments.length === 2) {
689 if (Array.isArray(val)) val = val.map(String);
690 else val = String(val);
691 if ('content-type' == field.toLowerCase() && !/;\s*charset\s*=/.test(val)) {
692 var charset = mime.charsets.lookup(val.split(';')[0]);
693 if (charset) val += '; charset=' + charset.toLowerCase();
695 this.setHeader(field, val);
697 for (var key in field) {
698 this.set(key, field[key]);
705 * Get value for header `field`.
707 * @param {String} field
712 res.get = function(field){
713 return this.getHeader(field);
717 * Clear cookie `name`.
719 * @param {String} name
720 * @param {Object} options
721 * @return {ServerResponse} for chaining
725 res.clearCookie = function(name, options){
726 var opts = { expires: new Date(1), path: '/' };
727 return this.cookie(name, '', options
728 ? merge(opts, options)
733 * Set cookie `name` to `val`, with the given `options`.
737 * - `maxAge` max-age in milliseconds, converted to `expires`
738 * - `signed` sign the cookie
739 * - `path` defaults to "/"
743 * // "Remember Me" for 15 minutes
744 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
747 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
749 * @param {String} name
750 * @param {String|Object} val
751 * @param {Options} options
752 * @return {ServerResponse} for chaining
756 res.cookie = function(name, val, options){
757 options = merge({}, options);
758 var secret = this.req.secret;
759 var signed = options.signed;
760 if (signed && !secret) throw new Error('cookieParser("secret") required for signed cookies');
761 if ('number' == typeof val) val = val.toString();
762 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);
763 if (signed) val = 's:' + sign(val, secret);
764 if ('maxAge' in options) {
765 options.expires = new Date(Date.now() + options.maxAge);
766 options.maxAge /= 1000;
768 if (null == options.path) options.path = '/';
769 var headerVal = cookie.serialize(name, String(val), options);
771 // supports multiple 'res.cookie' calls by getting previous value
772 var prev = this.get('Set-Cookie');
774 if (Array.isArray(prev)) {
775 headerVal = prev.concat(headerVal);
777 headerVal = [prev, headerVal];
780 this.set('Set-Cookie', headerVal);
786 * Set the location header to `url`.
788 * The given `url` can also be "back", which redirects
789 * to the _Referrer_ or _Referer_ headers or "/".
793 * res.location('/foo/bar').;
794 * res.location('http://example.com');
795 * res.location('../login');
797 * @param {String} url
798 * @return {ServerResponse} for chaining
802 res.location = function(url){
805 // "back" is an alias for the referrer
806 if ('back' == url) url = req.get('Referrer') || '/';
809 this.set('Location', url);
814 * Redirect to the given `url` with optional response `status`
817 * The resulting `url` is determined by `res.location()`, so
818 * it will play nicely with mounted apps, relative paths,
823 * res.redirect('/foo/bar');
824 * res.redirect('http://example.com');
825 * res.redirect(301, 'http://example.com');
826 * res.redirect('../login'); // /blog/post/1 -> /blog/login
831 res.redirect = function redirect(url) {
836 // allow status / url
837 if (arguments.length === 2) {
838 if (typeof arguments[0] === 'number') {
839 status = arguments[0];
840 address = arguments[1];
842 deprecate('res.redirect(url, status): Use res.redirect(status, url) instead');
843 status = arguments[1];
847 // Set location header
848 this.location(address);
849 address = this.get('Location');
851 // Support text/{plain,html} by default
854 body = statusCodes[status] + '. Redirecting to ' + encodeURI(address);
858 var u = escapeHtml(address);
859 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
868 this.statusCode = status;
869 this.set('Content-Length', Buffer.byteLength(body));
871 if (this.req.method === 'HEAD') {
879 * Add `field` to Vary. If already present in the Vary set, then
880 * this call is simply ignored.
882 * @param {Array|String} field
883 * @return {ServerResponse} for chaining
887 res.vary = function(field){
888 // checks for back-compat
889 if (!field || (Array.isArray(field) && !field.length)) {
890 deprecate('res.vary(): Provide a field name');
900 * Render `view` with the given `options` and optional callback `fn`.
901 * When a callback function is given a response will _not_ be made
902 * automatically, otherwise a response of _200_ and _text/html_ is given.
906 * - `cache` boolean hinting to the engine it should cache
907 * - `filename` filename of the view being rendered
912 res.render = function(view, options, fn){
913 options = options || {};
918 // support callback function as second arg
919 if ('function' == typeof options) {
920 fn = options, options = {};
924 options._locals = self.locals;
926 // default callback to respond
927 fn = fn || function(err, str){
928 if (err) return req.next(err);
933 app.render(view, options, fn);
936 // pipe the send file stream
937 function sendfile(res, file, options, callback) {
942 function onaborted() {
946 var err = new Error('Request aborted');
947 err.code = 'ECONNABORT';
952 function ondirectory() {
956 var err = new Error('EISDIR, read');
962 function onerror(err) {
981 function onfinish(err) {
982 if (err) return onerror(err);
985 setImmediate(function () {
986 if (streaming !== false && !done) {
998 function onstream() {
1002 file.on('directory', ondirectory);
1003 file.on('end', onend);
1004 file.on('error', onerror);
1005 file.on('file', onfile);
1006 file.on('stream', onstream);
1007 onFinished(res, onfinish);
1009 if (options.headers) {
1010 // set headers on successful transfer
1011 file.on('headers', function headers(res) {
1012 var obj = options.headers;
1013 var keys = Object.keys(obj);
1015 for (var i = 0; i < keys.length; i++) {
1017 res.setHeader(k, obj[k]);