Skip to content

Fixed: res.format crashing when only default option is provided - #2665

Closed
behcet wants to merge 2 commits into
expressjs:masterfrom
behcet:formatDefault
Closed

Fixed: res.format crashing when only default option is provided#2665
behcet wants to merge 2 commits into
expressjs:masterfrom
behcet:formatDefault

Conversation

@behcet

Copy link
Copy Markdown
Contributor

res.format crashes when provided only the default parameter.

Code:

 res.format({
'default': function () {
res.status(statusCode).send({ error: error });
}
});

Stack:

TypeError: Object application/json has no method 'replace'
at Mime.lookup (/expressApp/node_modules/express/node_modules/send/node_modules/mime/mime.js:70:18)
at exports.normalizeType (/expressApp/node_modules/express/lib/utils.js:103:21)
at ServerResponse.res.format (/expressApp/node_modules/express/lib/response.js:613:30)
at /expressApp/index.js:61:7

curl request
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -v htt p://127.0.0.1:5555/

After brief debugging response.js > res.format function I found that req.accepts() short circuits when !keys.length (which is zero, since we deleted obj.default) and returns internal function this.negotiator.mediaTypes(), which is in array type. Then it calls mime.lookup(type) on utils.js > exports.normalizeType with the array which we previously got from req.accepts, and crashes.

@dougwilson

Copy link
Copy Markdown
Contributor

Great find!

Comment threadlib/response.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just skip calling req.accepts when keys.length === 0

@dougwilsondougwilson mentioned this pull request Jun 19, 2015
6 tasks
@dougwilsondougwilson added this to the 4.13 milestone Jun 19, 2015
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@behcet@dougwilson