[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
When You try to do 301 redirections from a controller You get: "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" error. Headers are sent to a client to early.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at validateHeader (_http_outgoing.js:500:11)
at ServerResponse.setHeader (_http_outgoing.js:507:3)
at ServerResponse.header (/Users/adrian/projects/app/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/Users/adrian/projects/app/node_modules/express/lib/response.js:170:12)
at done (/Users/adrian/projects/app/node_modules/express/lib/response.js:1004:10)
at Immediate.<anonymous> (/Users/adrian/projects/app/node_modules/express-handlebars/lib/utils.js:26:13)
at runCallback (timers.js:756:18)
at tryOnImmediate (timers.js:717:5)
at processImmediate [as _immediateCallback] (timers.js:697:5)
Redirection will be done without any error.
@Get('/:param1(\\d+)/:param2?')
backwardCompatibility(@Param() params, @Response() res, @Headers() headers) {
const param1 = params.param1;
const param2 = params.param2;
ResponseHelper.redirect(res, headers,${param2}-${param1});
}
`export class ResponseHelper {
static redirect(res, headers, url) {
return res.redirect(HttpStatus.MOVED_PERMANENTLY, `https://${headers.host}/${url}`);
}
}`
After url format change I would like to create backward compatible request controller method to redirect user to the new url format.
Nest version: 5.0.1
For Tooling issues:
- Node version: v9.2.0
- Platform: Mac
Others:
Hi AdrianBudzynski, I have met the same question, and I saw you closed the issue, so could you please show me how did you solve the problem? THANKS~
@liupy525
I did it the following way.
@Get("MyRoute")
@ApiResponse({ status: HttpStatus.FOUND, description: "Redirection to the front-end" })
myRoute(
@Query('someQueryParam') someQueryParam: string,
@Res() res: Response,
): void {
const redirectUrl =
new URL(`${process.env.FRONT_END_HOSTING_PATH}/SomeFrontEndRoute`);
/** redirectUrl.search = .... */
res.redirect(redirectUrl.toString());
}
Add
import * as express from 'express';
and use
@Post('myEndpoint')
postAuthRequest(@Response() response: express.Response) {
return response.redirect(303, `http://redirect.com`);
}
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Add
and use