From 2151c6452d00e5b17f8e8a6cd86f72549e85f66c Mon Sep 17 00:00:00 2001 From: vaibhavmashal Date: Tue, 25 Aug 2026 22:19:16 +0530 Subject: [PATCH 1/2] fix(res): remove magic from res.set() Content-Type --- lib/response.js | 11 +++-------- test/res.send.js | 4 ++-- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/response.js b/lib/response.js index bc672434890..dc2fef6ca69 100644 --- a/lib/response.js +++ b/lib/response.js @@ -241,7 +241,7 @@ res.json = function json(obj) { // content-type if (!this.get('Content-Type')) { - this.set('Content-Type', 'application/json'); + this.type('json'); } return this.send(body); @@ -271,7 +271,7 @@ res.jsonp = function jsonp(obj) { // content-type if (!this.get('Content-Type')) { this.set('X-Content-Type-Options', 'nosniff'); - this.set('Content-Type', 'application/json'); + this.type('json'); } // fixup callback @@ -282,7 +282,7 @@ res.jsonp = function jsonp(obj) { // jsonp if (typeof callback === 'string' && callback.length !== 0) { this.set('X-Content-Type-Options', 'nosniff'); - this.set('Content-Type', 'text/javascript'); + this.type('text/javascript'); // restrict callback charset callback = callback.replace(/[^\[\]\w$.]/g, ''); @@ -655,9 +655,6 @@ res.append = function append(field, val) { * * Aliased as `res.header()`. * - * When the set header is "Content-Type", the type is expanded to include - * the charset if not present using `mime.contentType()`. - * * @param {String|Object} field * @param {String|Array} val * @return {ServerResponse} for chaining @@ -671,12 +668,10 @@ res.header = function header(field, val) { ? val.map(String) : String(val); - // add charset to content-type if (field.toLowerCase() === 'content-type') { if (Array.isArray(value)) { throw new TypeError('Content-Type cannot be set to an Array'); } - value = mime.contentType(value) } this.setHeader(field, value); diff --git a/test/res.send.js b/test/res.send.js index f38ffde9776..f6930f326d6 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -200,7 +200,7 @@ describe('res', function(){ request(app) .get('/') - .expect('Content-Type', 'text/plain; charset=utf-8') + .expect('Content-Type', 'text/plain') .expect(200, 'hey', done); }) @@ -213,7 +213,7 @@ describe('res', function(){ request(app) .get("/") - .expect("Content-Type", "text/plain; charset=utf-8") + .expect("Content-Type", "text/plain") .expect(200, "hey", done); }) From 5ab54bc5ea8921f9706bcd8df2924dec40e9bfc5 Mon Sep 17 00:00:00 2001 From: vaibhavmashal Date: Mon, 31 Aug 2026 21:05:10 +0530 Subject: [PATCH 2/2] fix(format): migrate res.format to res.type for Content-Type expansion --- lib/response.js | 20 ++++++++------------ test/res.format.js | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/response.js b/lib/response.js index dc2fef6ca69..9b2b4fd359c 100644 --- a/lib/response.js +++ b/lib/response.js @@ -506,7 +506,7 @@ res.contentType = res.type = function contentType(type) { var ct = type.indexOf('/') === -1 ? (mime.contentType(type) || 'application/octet-stream') - : type; + : (mime.contentType(type) || type); return this.set('Content-Type', ct); }; @@ -535,25 +535,21 @@ res.type = function contentType(type) { * res.send('

hey

'); * }, * - * 'application/json': function () { + * 'application/json': function(){ * res.send({ message: 'hey' }); * } * }); * - * In addition to canonicalized MIME types you may - * also use extnames mapped to these types: + * In addition to an object of callbacks you may also provide a function + * which will be invoked when no match is made and the "Accept" header is + * present, otherwise 406 "Not Acceptable" is sent. * * res.format({ * text: function(){ * res.send('hey'); * }, - * - * html: function(){ - * res.send('

hey

'); - * }, - * - * json: function(){ - * res.send({ message: 'hey' }); + * default: function(){ + * res.send('hey'); * } * }); * @@ -582,7 +578,7 @@ res.format = function(obj){ this.vary("Accept"); if (key) { - this.set('Content-Type', normalizeType(key).value); + this.type(normalizeType(key).value); obj[key](req, this, next); } else if (obj.default) { obj.default(req, this, next) diff --git a/test/res.format.js b/test/res.format.js index 0d770d57651..13ba0ff84ba 100644 --- a/test/res.format.js +++ b/test/res.format.js @@ -176,6 +176,23 @@ describe('res', function(){ test(app) }) + + it('should set Content-Type with charset when formatter uses res.end()', function (done) { + var app = express(); + + app.use(function (req, res) { + res.format({ + 'text/plain': function () { + res.end('ok'); + } + }); + }); + + request(app) + .get('/') + .expect('Content-Type', 'text/plain; charset=utf-8') + .expect(200, 'ok', done); + }) }) })