-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Stream the http response #213
Comments
There are a couple of issues related to our strong-remoting stream support.
Maybe we should plug in an adapter into strong-remoting to handle multipart/form-data requests and have the storage-service upload function to be configured to the 'stream' binding. |
👍 This is my take on the API for stream responses (not a final version, just an idea): function streaming(cb) {
var bodyStream = // create response stream from file
var contentType = // set from file extension
cb(null, bodyStream, contentType);
}
loopback.remoteMethod(streaming, {
returns: [
{ arg: 'bodyStream', type: 'stream', root: true },
{ arg: 'content-type', type: 'string', http: { target: 'header' } }
]
}); Not sure what API to use for request streams and multi-part requests. |
+1 |
Similar discussion: strongloop/strong-remoting#35 |
Hi, any update on this issue? is there a example of how to implement download file functionality with loopback? |
@haio We don't have a proper support for streams implemented yet. The workaround of passing the HTTP response object to the remoted function should work though - see the links to loopback-storage-services in the issue description for more details. File.download = function(file, res, cb) {
var reader = fs.createReadStream('storage/' + file);
reader.on('error', function (err) {
res.type('application/json');
res.send(500, { error: err });
});
reader.pipe(res);
// callback is intentionally not invoked
};
File.remoteMethod('download', {
accepts: [
{arg: 'file', type: 'string', 'http': {source: 'path'}},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
],
http: {
verb: 'get',
path: '/download/:file'
}
}); |
@bajtos I follow your way and see it works, thanks! |
Hi, I followed your server side implementation example to implement my own streaming from mongoDb gridfs storage, but I would be very interesting by the client side solution. |
I found the issue for my concern... |
Could you share how you implemented gridfs storage? Thanks. |
Closing in favour of follow-up issues: strongloop/loopback-swagger#34 and #2209 |
Hi bajtos I'm currently trying to download a file from the backend API(loopback3). UserProfile.remoteMethod ( UserProfile.totalDeductions = function(sDate, eDate, res, cb) {
But I'm not able to download a file. It still returns response as a contect, not a file. |
@web-hollywood I don't see any obvious problem. Please create a small app that I can use to reproduce the issue on my computer (see http://loopback.io/doc/en/contrib/Reporting-issues.html#bug-report) and open a new issue. Thanks! |
Consider a remotable method that returns a stream as the result, for example "download file" in loopback-storage-service (source).
The current implementation takes the response object as an argument and does not call the provided callback. This means that "after" hooks are never called. (remoting metadata).
We should make streams a first-class citizen in strong-remoting.
HttpContext.prototype.invoke
seems to already support piping a stream provided by the remotable method to the HTTP response (source).Tasks
A note for loopback-swagger support. Swagger spec says: http://swagger.io/specification/#responseObject
I don't remember whether loopback-swagger supports per-method
produces
option. If it does not, then we should implement it as part of this work.Related: strongloop/loopback-component-explorer#103
The text was updated successfully, but these errors were encountered: