I work on a REST API using Node, Express and TypeScript. I want to write a controller for downloading a file from the server. The filename is saved in a database alongside its extension, path, etc. The id is given as request parameter. Somehow I can't manage to make it work, after trying res.download()
and other solutions. Here is my code:
@Get('/:uuid/download')
async downloadFile(@Param('uuid') uuid: string, @Res() res: Response) {
const result = await this.fileService.getFileById(uuid);
if (result !== undefined) {
const file = join(result.path, result.name + result.extension);
res.download(file);
return res;
} else {
return res.status(404).json({message: 'File not found'});
}
}
I get no error in logger and a "Cannot GET /api/file/some-uuid/download" in browser. I tried with a local file from the working directory but again with no success. Also, I tried other solutions and I keep getting this error:
error: - Error
at NotFoundError.HttpError [as constructor] (C:\Users\Teo\Desktop\projects\cyberbox\back\node_modules\routing-controllers\http-error\HttpError.js:31:23)
at new NotFoundError (C:\Users\Teo\Desktop\projects\cyberbox\back\node_modules\routing-controllers\http-error\NotFoundError.js:24:28)
at ExpressDriver.handleSuccess (C:\Users\Teo\Desktop\projects\cyberbox\back\node_modules\routing-controllers\driver\express\ExpressDriver.js:316:23)
at C:\Users\Teo\Desktop\projects\cyberbox\back\node_modules\routing-controllers\RoutingControllers.js:136:61
at processTicksAndRejections (node:internal/process/task_queues:93:5)
If someone know or has any idea, would save me for days of struggle. Thank you!