How to sendFile in NotFoundException using fastify

I'm using @fastify/static so I could use sendFile in reply of fastify and that works perfect with Controller like (I should return cause without return files are sending empty)

@Controller()
export class AppController {
  @Get('file')
  getFileTest(@Res() res: FastifyReply) {
    return res.sendFile('index.html');
  }
}

but the problem is I need to sendFile in an exception (and all files are sending empty no matter with return or without) (that works with express and not with fastify)

import {
  ExceptionFilter,
  Catch,
  NotFoundException,
  ArgumentsHost,
} from '@nestjs/common';

import { FastifyReply } from 'fastify';

@Catch(NotFoundException)
export class NotFoundExceptionFilter implements ExceptionFilter {
  catch(exception: NotFoundException, host: ArgumentsHost) {
    const response = host.switchToHttp().getResponse<FastifyReply>();
    return response.status(200).sendFile('index.html');
    //response.status(200).send('text'); //works fine express and fastify
  }
}

are there ways to sendFile from and @Catch(NotFoundException)?

route "/" returns index.html
route "file" returns index.html
any else routes are returning empty index.html

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest framework TypeScript starter repository.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.