Deprecate res.send(status, body) format
This is a discussion regarding the possibility of deprecating res.send(status, body) and other methods (json, jsonp to name some) that do the same thing, where there is an optional first status argument.
Specifically, deprecating the optional status as a first argument thing.
/cc @defunctzombie
kill them all!
For the record, at this time @defunctzombie is for it, and I am for it; taking optional first arguments is a really annoying API in JavaScript. The worst part about the current API is what does res.send(num) do? Right now num is treating as a status code, but of course there is no way to disambiguate it from body being the num--it's just a magic rule you have to know.
With fire!
Anyway else watching want to chime in (mostly to say nay, since there are already a few yays)?
:fuelpump: :bomb: :fire: I don't like this convention either. It's odd and confusing.
:thumbsup: . res.status(code) is clear, concise, chainable, great. It should be the canonical thing in express. Magical APIs that do too much introspection to their arguments or arguments.length create a lot of errors and confusion, especially for newbies.
Alright. I'm officially saying this is no longer an idea and is scheduled to be in 4.7.
Hm. I'm actually starting to become inclined to un-deprecate this signature (and maybe possibly deprecating not including the status). The issue I'm seeing is that res.send(status, body) actually makes a lot of sense for an API-driven site. A standard site would use res.render.
What do you think, @nolanlawson ?
So for everyone: I'm actually leaning more towards making status a mandatory argument, rather than an optional one or not there at all. Having the status really makes sending some data pretty concise, and there are a lot of times where you do want a particular status to be sent (more than just "whatever the status just happens to be set to on the response). Thoughts?
I still think you should explicitly res.status always.
I still think you should explicitly res.status always.
Right, thus my idea of actually making the status a mandatory argument to res.send and friends. Otherwise you basically end up always writing the kinda dumb-looking chain instead (res.status(200).send('hi')). The chain also does have a weirdness to it: you have to always do the .status() before the .send() currently, for example.
Right, thus my idea of actually making the status a mandatory argument to res.send and friends.
Makes sense for REST API servers, doesn't make sense for servers that always respond with 200 or 404. I've seen both.
Yeah, mandatory status might be better (unless there are cases when status is set in one function, but response is returned in other one).
Basically my main thoughts right now are to remove the deprecation in 4.6.1 and discuss some more about each side.
doesn't make sense for servers that always respond with 200 or 404.
Hm, yea, idk. So like I just don't know :( The main problem right now is that the signature morphs depending on the number of arguments, and especially since it is the first argument that is optional.
So like, one common idiom I see is if (err) return res.send(500, 'oops!'), which is the nice one-line bailout people like to write. Now, arguably they should probably be using the error pipeline, of course.
At the same time, the status code of a response is almost like fundamental to the HTTP response itself, so it does seem logical to pass in the code and the body in the same spot, especially for something like res.json().
My two cents:
- 200 is a reasonable default for the status code
- +1 un-deprecate res.send(status, entity)
I am likely to revert the deprecation in 4.7.1 (so the deprecation would basically only be warning in 4.7.0) for more discussions. This deprecation was actually a pretty large one in terms of the express API, and as I am re-writing more stuff to comply, it's looking like it's actually not for the better (user experience).
We do need to get rid of the "optional first argument" thing, though, so I'd like to see the debate between removing the status vs requiring the status in res.send, res.json, etc.
I think deprecating this was the right move. When I want to send things that are not 200 I either set the status, or use http error classes.
I think 200 is a sensible default status and if your logic requires another status then it is good to be explicit. I dislike magic numbers in function calls and this feels like one of those if you leave the status parameter in the send.
+1 for leaving deprecated and moving forward. On Jul 26, 2014 2:56 PM, "Douglas Christopher Wilson" < [email protected]> wrote:
I am likely to revert the deprecation in 4.7.1 (so the deprecation would basically only be warning in 4.7.0) for more discussions. This deprecation was actually a pretty large one in terms of the express API, and as I am re-writing more stuff to comply, it's looking like it's actually not for the better (user experience).
We do need to get rid of the "optional first argument" thing, though, so I'd like to see the debate between removing the status vs requiring the status in res.send, res.json, etc.
— Reply to this email directly or view it on GitHub https://github.com/visionmedia/express/issues/2227#issuecomment-50249768 .
or use http error classes.
Really, this is the right answer for all codes >= 400. One of the things I have on my plate to do it give a module that makes it easy for people to create these errors with the properties express and friends expect. One barrier that I realized earlier today is that currently the default action for errors in express is to console.error then, which sucks if you start pumping lots of 4xx errors through the pipeline :)
I dislike magic numbers in function calls and this feels like one of those if you leave the status parameter in the send.
this
I always provide an error handler.
Usually I use this module for my error classes. https://www.npmjs.org/package/httperrors
But something that plays nicer with express could be good.
Question: I have an REST api project and am getting deprecation warnings all over because in this type of project it's very necessary to constantly set the status to whatever is appropriate so this is perhaps a larger issue for this scenario than a simple static web server.
Before I start making so many changes to satisfy the deprecation it appears there is some discussion to not deprecating it after all. Now I'm not sure, should I change my code or should I just leave it?