Nest: [Fastify-Adapter] Injecting @Res() results in timeout of endpoint

Created on 8 Oct 2019  路  6Comments  路  Source: nestjs/nest

Bug Report

Current behavior

When using Fastify-Adapter and then injecting fastify response via @Res() decorator in controller method results in timeout of that endpoint (even when sending a response via res.send(...))

Input Code

https://github.com/mschuttt/bug-nest-fastify-adapter

nest new bug-fastify
remove @nestjs/platform-express from package.json
npm i --save @nestjs/platform-fastify

change main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestFastifyApplication, FastifyAdapter } from '@nestjs/platform-fastify';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
  await app.listen(3000);
}
bootstrap();

change app.controller.ts

import { Controller, Get, Res } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(@Res() res): void {
    res.send('hello')
  }
}

npm run start
browser or postman: localhost:3000 <- endpoint times out.. no response!

(refer to git repo above..)

Expected behavior

When sending a response (res.send(...)) - endpoint should send a response!

Environment


Nest version: 6.8.2


For Tooling issues:
- Node version: v10.15.3
- Platform:  Linux Ubuntu 16.04
needs clarification

Most helpful comment

Could you check out my provided sample repo, please? The example provided under sample (10-fastify) doesn't` work for me neither when I add an endpoint with the @Res() decorator

My sample endpoint in cats controller

  @Get('testo')
  hello(@Res() res) {
    res.send('hallo')
  }

the only way I get it to work is to downgrade the @nestjs packages

| @nestjs/common | @nestjs/core | @nestjs/platform-fastify | Response |
| ------------- | ------------- | ------------- | ------------- |
| 6.8.2 | 6.8.2 | 6.8.2 | No |
| 6.7.2 | 6.7.2 | 6.7.2 | Yes |

This is only an issue with 6.8.2

All 6 comments

I just verified the behavior with the fastify sample which is provided in the repo under sample directory and everything works well. I'd suggest updating all dependencies, removing node_modules and reinstalling everything from scratch.

Could you check out my provided sample repo, please? The example provided under sample (10-fastify) doesn't` work for me neither when I add an endpoint with the @Res() decorator

My sample endpoint in cats controller

  @Get('testo')
  hello(@Res() res) {
    res.send('hallo')
  }

the only way I get it to work is to downgrade the @nestjs packages

| @nestjs/common | @nestjs/core | @nestjs/platform-fastify | Response |
| ------------- | ------------- | ------------- | ------------- |
| 6.8.2 | 6.8.2 | 6.8.2 | No |
| 6.7.2 | 6.7.2 | 6.7.2 | Yes |

This is only an issue with 6.8.2

Can confirm the above mentioned issue. We had it in one of our projects and switched back to ExpressAdapter, no issues there on 6.8.2.

Using FastifyAdapter on 6.8.2 the affected controller with @Res injection times out and throws an undefined exception when the endpoint is called, while on 6.7.2 the injection of the response object works as expected.

The same issue occurs with @mschuttt s example application. I have cloned it, fixed all @nestjs dependency versions to 6.8.2 in package.json, run npm i and executed the application. This is the output after approx. 2.5min of timeout on the request:

[Nest] 20802   - 10/09/2019, 10:06:03 PM   [NestFactory] Starting Nest application...
[Nest] 20802   - 10/09/2019, 10:06:03 PM   [InstanceLoader] AppModule dependencies initialized +18ms
[Nest] 20802   - 10/09/2019, 10:06:03 PM   [RoutesResolver] AppController {/}: +8ms
[Nest] 20802   - 10/09/2019, 10:06:03 PM   [RouterExplorer] Mapped {/, GET} route +8ms
[Nest] 20802   - 10/09/2019, 10:06:03 PM   [NestApplication] Nest application successfully started +4ms
[Nest] 20802   - 10/09/2019, 10:08:35 PM   [ExceptionsHandler] Cannot read property 'send' of undefined +152457ms
TypeError: Cannot read property 'send' of undefined
    at AppController.getHello (/home/mvecchio/source/bug-nest-fastify-adapter/dist/app.controller.js:22:13)
    at /home/mvecchio/source/bug-nest-fastify-adapter/node_modules/@nestjs/core/router/router-execution-context.js:37:29

Doing the same and setting all versions to 6.7.2 the application works as expected.

Facing the same issue after upgraded to 6.8.2. This snippet from docs does not work anymore:

@Get()
index(@Res() res) {
  res.status(302).redirect('/login');
}

Fixed in 6.8.3. Please, let me know if you face any issue.

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

mishelashala picture mishelashala  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments

hackboy picture hackboy  路  3Comments

FranciZ picture FranciZ  路  3Comments

tronginc picture tronginc  路  3Comments