Some thoughts on returning json / text
See original GitHub issueI like this idea a lot, especially when it comes to error handling.
However, by far the most common use case would be to send json or text. What would you think about the following:
- when the promise rejects, call
next(err)
- when the promise resolves with text, call
res.send(text)
- when the promise resolves anything else, call
res.json(obj)
next()
andnext('route')
seem like exceptional cases to me, what about just not handling those?
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How do I return JSON in HTTP response? - ReqBin
To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a...
Read more >MVC: How to Return a String as JSON - Stack Overflow
The contents of the file are pre-serialized to JSON. This is to ensure that there is nothing standing in the way of the...
Read more >rest api - JSON String in response shows up with quotes
The client call to the REST API returns with qoutes around the reqult like this example: "...result.." and also with \" inside the...
Read more >Format response data in ASP.NET Core Web API
For example, returning JsonResult returns JSON-formatted data and returning ContentResult returns plain-text-formatted string data. An action ...
Read more >JSON.stringify() - JavaScript - MDN Web Docs
For example, JSON.stringify on the same object will always produce the same string, and JSON.parse(JSON.stringify(obj)) would produce an object ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This seems like dangerous functionality. Especially with implicit returns (ES6 arrow functions, CoffeeScript, …), it would be very easy to accidentally return (and thus expose) data that the user is never meant to see.
As the last comment is over a year old i’d like to close this issue (and #11). But if anyone is interested in pushing these thoughts in a new direction (eg. by wrapping the response or by configuring this functionality “opt-in”) we could open a new issue.