InversifyJS
InversifyJS copied to clipboard
express-utils - request aborted
Hi.
When I try to send a file, I receive virtually nothing on the client side.
The code for sending is pretty simple:
@httpGet("/:resid/:attachment/download")
public downloadFile(@response() resp: Response, @requestParam("resid") resid: string, @requestParam("attachment") file: string): Promise<void> {
return this.getFilePath(resid).then( (path) => {
resp.sendFile(path, (err) => {
console.log("An error occurred:");
console.log(err);
console.log(`The path was: ${path}`);
});
});
}
Hovewer, running this gives me following:
An error occurred:
{ Error: Request aborted
at onaborted (/root/nets/node_modules/express/lib/response.js:1021:15)
at Immediate._onImmediate (/root/nets/node_modules/express/lib/response.js:1063:9)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5) code: 'ECONNABORTED' }
I was curious, if browser isn't closing the connection, but it looks like problem on server, as I see this on network:
HTTP/1.1 204 No Content
X-Powered-By: Express
Date: Fri, 15 Feb 2019 19:00:23 GMT
Server: lighttpd/1.4.50
Maybe I'm doing something wrong, but I'm unable to figure out what is it.
- Node v10.13.0
- OpenWRT/Linux turris 4.4.161-6115ddc22e304d6f91eb3b4b129b8a73-1 #1 SMP armv7l n
Thank you and have a nice day. If you need some more info, just tell me. Thanks!
I am also having this issue!
+1
Hello.
I work right now on different project. Here I needed to send files too and I managed to solve it pretty much easily. The easiest way is to return never resolving promise.
Because, when you return nothing directly or resolve promise before actually sending file contents, the inversify-express-utils causes sending empty response and closing request.
So, the workaround (or correct way of use) is following:
@httpGet(API.Urls.Photos.thumbnail)
public async getThumb(@requestParam("id") id: string, @response() resp: express.Response) {
this.photosrv.getPhotoById(id)
.then((itm) => {
let file = path.join(CFG.thumbPath, itm.folder, itm.thumb);
resp.sendFile(file);
});
return new Promise((res, rej) => {});
}
I'm also having the same issue. The suggestion from @sukovec worked fine, but I came up with another workaround to this issue:
const html = fs.readFileSync('${staticPath}/unsubscribe.html', "utf8"); return res.send(html);
same here
I believe the correct way of doing it is:
return new Promise<void>((resolve, reject) => {
resp.sendFile(file, (err) => {
if (!err) resolve()
else reject(err)
})
})