-
Notifications
You must be signed in to change notification settings - Fork 116
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
Possibility to handle response with @response object #20
Comments
Thank you very much for the kind words! I think there may be two separate issues here (one on Restivus' end, and one on yours). I definitely intend for users to be able to handle the [low-level responses] with "Could there be a parameter that would indicate that method will not return anything and will take care of ending the response? Restivus logs following error: TypeError: Cannot read property 'body' of null at [object Object].Router.route.action (packages/nimble:restivus/lib/route.coffee:53:24)" I can't think of any clean way to use a parameter in the endpoint to cause Restivus to ignore the data returned from the endpoint. If you have any suggestions (preferably with some example code), I would love to hear them. I think there may be an acceptable way around this though. As it stands, returning null from an endpoint is treated as an error. My thought there was that something should always be returned from a REST endpoint. Of course, that was an oversight, since a user handling the low-level response should be able to return their own data and prevent Restivus from attempting to send another response, causing an error (as you suggested). Intuitively, you would think to just return null in that situation (as you did), since you've already handled the response, and after giving it some thought I think that's the right way to go. Handling the null response instead of treating it like an error was quite straight forward, and I've already implemented and tested that on a local branch. This allows you to handle the response manually with Right now I'm in the process of implementing some pretty handy response hooks (to allow you to call functions at predefined points before, during, and after execution of your endpoints), and one of the hooks is the I've left it so that returning That was a lot to take in, but that covers the issues being caused by Restivus. As for the issue on your end: Restivus.addRoute 'test', { authRequired: false }, get: ->
_this = @
_this.response.write("test1")
setTimeout(->
_this.response.write("test")
_this.response.end()
, 1000)
return null "The above code writes "test1" to the client, but not "test", probably as restivus has already closed the connection." The reason that I will push these changes up soon for you to test out. I'll keep you posted. Let me know if any of that was unclear, or if you have any other questions or suggestions. |
Sorry for not publishing this update yet. I wasn't really happy with simply returning null from an endpoint to indicate that the response had been handled manually. Returning null may be done by accident, and in that case the response would be sent back with an empty body and no indication of any error. Instead, I would prefer to continue to treat a null return value as an error. I spoke with @anthonyreid99 and we came up with a much cleaner solution. I won't bore you with the details just yet, but I should be able to implement it within the next few days and get this update out. The API should be very close to the one proposed here. I'll keep you posted. Sorry about the delay on this. |
Sorry about all of those commit references. Just had to rebase a bunch to get the markdown right for the docs. Forgot that I had referenced the issue. Anywho, the latest release, v0.6.3 (along with my suggestions above) should fix this issue. I added a section to the docs to explain how to handle the response manually from within your endpoints. The only difference is that you just need to call Please let me know if you have any other questions or issues. Thank you for taking the time to report this! |
Thank you! |
First of all, thanks for a great package. Very easy to setup and functions well.
Now to the problem: I'd like to use restivus for both REST-stuff, and also for a few lower-level HTTP calls where I'm writing back binary data in chunks at a callback. I know that's not really anymore the scope of this package (REST), but as it 'almost' seems to work, would it be possible to implement?
Basically, in get() function, I hooked to @response object, and write(..) data there. After it's done, I do @response.end().
The above code writes "test1" to the client, but not "test", probably as restivus has already closed the connection. Could there be a parameter that would indicate that method will not return anything and will take care of ending the response?
Also, restivus logs following error:
TypeError: Cannot read property 'body' of null at [object Object].Router.route.action (packages/nimble:restivus/lib/route.coffee:53:24)
The text was updated successfully, but these errors were encountered: