Nest: Allow to redirect from a controller

Created on 19 Jun 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] 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.

Current behavior


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)

Expected behavior


Redirection will be done without any error.

Minimal reproduction of the problem with instructions


@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}`);
}

}`

What is the motivation / use case for changing the behavior?


After url format change I would like to create backward compatible request controller method to redirect user to the new url format.

Environment


Nest version: 5.0.1


For Tooling issues:
- Node version: v9.2.0  
- Platform: Mac 

Others:

Most helpful comment

Add

import * as express from 'express';

and use

@Post('myEndpoint')
  postAuthRequest(@Response() response: express.Response) {
    return response.redirect(303, `http://redirect.com`);
  }

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breitsmiley picture breitsmiley  路  3Comments

rlesniak picture rlesniak  路  3Comments

anyx picture anyx  路  3Comments

artaommahe picture artaommahe  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments