-
Notifications
You must be signed in to change notification settings - Fork 397
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
response.sendFile #288
Comments
Try to use |
You have to wait for #286. |
If I use this code public async getImage(@Res() response: express.Response, @Params() params) {
try {
let filePath = path.join(path.normalize(ImagesFolder), params.path);
response.type("jpg").send();
} catch (err) {
}
} I get the error
Is this also caused by the issue that gets fixed in #286? |
Yes, routing-controllers is trying to send the response even if you already send it using |
Is there a ETA for this PR? |
@NoNameProvided and @pleerock have to review this PR, then it will be released with other merged PRs. For now you can compile the npm package by yourself. |
@19majkel94 can it be closed now? |
I've updated to:
I get error |
You need to return response, not void, which is the result of calling sendFile(path: string): void;
sendFile(path: string, options: any): void;
sendFile(path: string, fn: Errback): void;
sendFile(path: string, options: any, fn: Errback): void; |
so how can solve it |
@Get('/File/:name')
async GetFile(@Req() request: Request, @Res() response: Response) {
let filePath = path.join(__dirname, '../public', request.params.name);
console.log('File: ', filePath);
response.sendFile(filePath);
return response;
} |
@19majkel94 I use your code,but it do not work, the framework still try to callback my CustomErrorHandler, which is implements ExpressErrorMiddlewareInterface。so I use Response.headersSent in my CustomErrorHandler to judge whether the Response has been sent. the code look like this; export class CustomErrorHandler implements ExpressErrorMiddlewareInterface {
error(error: any, req: Request, res: Response) {
// if (res.statusCode === 200) return;
if (res.headersSent) return;
res.status(500);
res.json({
status: 'fail',
error: error.message
});
}
}
@Middleware({ type: 'after' })
export class CustomNotFoundHandler implements ExpressMiddlewareInterface {
use(req: Request, res: Response) {
// if (res.statusCode === 200) return;
if (res.headersSent) return;
res.status(404);
res.json({
status: 'fail',
error: 'Not Found'
});
}
} |
I don't know what "it" is, please open new issue and describe your problem with code samples. |
@aasailan sendFile basically opens a stream. You should await on it and return response only then.
It would be better to promisify the prototype of Response but I'm not sure how to do that in this case |
@keenondrums Right, I was just showing and example of returning response object, not a working solution 😉 For cases like serving spa's index.html it's better to use serve-static rather than custom routes with sendFile. |
@19majkel94 you can't use only serve static. You have to match every route except your api routes to index html. I combine them to serve other static files like js with static middleware and add serveSpa route to serve index.html |
|
@19majkel94 works lie a charmm. Thnx for the info! |
#376 solved the problem and we have to return response (not return ;)to avoid the double return problem: ' Can't set headers after they are sent '. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
If I use response.sendFile("/file") I get the following error:
The file exists and is readable
The text was updated successfully, but these errors were encountered: