Skip to content
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

The asynchronous middleware does not work within controller #929

Closed
yangjdb opened this issue Aug 1, 2018 · 4 comments
Closed

The asynchronous middleware does not work within controller #929

yangjdb opened this issue Aug 1, 2018 · 4 comments

Comments

@yangjdb
Copy link

yangjdb commented Aug 1, 2018

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

The Asynchronous middleware does not work within controller
code e.g:

middleware file:

@Injectable()
export class ResponseMiddleware implements NestMiddleware {
    async resolve(name: string): Promise<MiddlewareFunction> {
        return async (req, res, next) => {
            try {
                console.log(`Request...`, name);     //step into breakpoint 1
                res.state ? void(0) : res.state = {};   //step into breakpoint 2
                await next();                   //step into breakpoint 3
                res.json(res.body ? res.body : {       //step into breakpoint 5
                    code: res.state.code !== undefined ? res.state.code : 0,
                    data: res.state.data !== undefined ? res.state.data : {},
                });
            } catch (e) {
                console.error('Catch Error: %o', e);
                res.status(200);
                res.json({
                    code: -1,
                    error: e && e.message ? e.message : e.toString()
                });
                // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
            }
        }
    }

}

module file:

export class WxappModule {
    configure(consumer: MiddlewareConsumer) {
        consumer.apply(ResponseMiddleware).forRoutes(WxappController);
    }
}

controller file:

@Controller('weapp')
export class WxappController {

    @Get('')
    public async root(@Req() req, @Res() res): Promise<any> {
        res.state.data = {       //step into breakpoint 4
            msg: 'hello',
        };
    }
}

Expected behavior

To visit url "/weapp", the result of my expectation is step 1->2->3->4->5, but the actual result is 1->2->3->5->4 and even this request being hang up, no response return

Minimal reproduction of the problem with instructions

Asynchronous middleware does not work within controller.

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

I want to change the response's object by using middleware

Environment


Nest version: 5.1.0

 
For Tooling issues:
- Node version: 8.9.3  
- Platform: Win64 

Others:
webstorm breakpoint debug

@ryanswart
Copy link

Nest middleware is designed to be run before route handlers; use Interceptors to modify the response https://docs.nestjs.com/interceptors

@kamilmysliwiec
Copy link
Member

@ryanswart +1

@yangjdb
Copy link
Author

yangjdb commented Aug 2, 2018

thx

@lock
Copy link

lock bot commented Sep 24, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants