Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set 'json spaces' on res.json #2469

Closed
wants to merge 1 commit into from
Closed

Add ability to set 'json spaces' on res.json #2469

wants to merge 1 commit into from

Conversation

jczaplew
Copy link

Currently json spaces can only be set at an application level, but it would be nice to be able to override that setting on the response level. For example, in my situation I'd like the root of an API route to pretty print JSON (app.set('json spaces', 2)), but an actual response to be minified (app.get('json spaces', 2).

I found a way to get around this by setting the json spaces on a response as so:

res
   .set("Content-type", "application/json; charset=utf-8")
   .send(JSON.stringify(data, null, 2));

This works well, but it would be nice to set json spaces directly on res.json in the same way you can set something like Content-type. This PR allows you to do this - for example:

res
   .set("json spaces", 2)
   .json(data);

If json spaces is not set on a given response it falls back to the application's json spaces setting.

@dougwilson
Copy link
Contributor

res.set and res.get is not a bag of keys; you're actually setting a header value because res.set("json spaces", 2) will add the header json-spaces: 2 to your response.

@dougwilson dougwilson added the pr label Dec 12, 2014
@jczaplew
Copy link
Author

Ah! I didn't notice that. Thanks! I'll have to rethink this...

@jczaplew jczaplew closed this Dec 12, 2014
@dougwilson
Copy link
Contributor

No problem :) Feel free to comment here as well: #2422 which has some more discussion, also acknowledging this is an issue we'd like to support :)

@jczaplew
Copy link
Author

Great! Thanks! I searched around for a discussion or information on this topic and couldn't find that thread. That helps a lot!

@rlidwka
Copy link
Member

rlidwka commented Dec 12, 2014

New feature added, no tests for it, but code coverage remains the same 100%. Cool. :)

Sign up for free to 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.

3 participants