kyle-mccarthy / nest-next

Render Module to add Nextjs support for Nestjs
MIT License
644 stars 91 forks source link

got a lot of 404 error #100

Open chenzheio opened 1 year ago

chenzheio commented 1 year ago

when I use HttpExceptionFilter in nestjs to handler exception.

I saw a lot of 404 error in terminal.

I've try change config "passthrough404" in RenderModule register but it seem not work.

Path:src/app.module.ts

import { Module } from '@nestjs/common';
import { RenderModule } from 'nest-next';
import next from 'next';
import { AppController } from './app.controller';
import { AdminModule } from './admin/admin.module';

@Module({
  imports: [
    RenderModule.forRootAsync(
      next({
        dev: process.env.NODE_ENV !== 'production',
        conf: { useFileSystemPublicRoutes: false }
      }), {
      passthrough404: true, // I've try to change to false, but I still got 404 error.
    }),
    AdminModule
  ],
  controllers: [AppController],
  providers: [],
})
export class AppModule { }

Path: src/main.ts

import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Request } from 'express';
import { AppModule } from './app.module';

class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: { response: { statusCode: number, message: string, error: string } }, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request: Request = ctx.getRequest();

    if (exception.response.statusCode === 401) {
      return response.redirect(request.url.includes('admin') ? '/admin/login' : '/login');
    }

    console.log(exception.response);

    return response
      .render("CustomError", {
        timestamp: new Date().toISOString(),
        path: request.protocol + "://" + request.hostname + request.url,
        ...exception.response
      })
  }
}

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.useGlobalFilters(new HttpExceptionFilter());
  await app.listen(3000);
}
bootstrap();
Xnip2023-01-11_16-14-36

dependencies:

"@nestjs/core": "^9.0.0",
"nest-next": "^10.0.0",
"next": "^13.1.1",
kyle-mccarthy commented 1 year ago

does this happen when you don't enable your HttpExceptionFilter? also can you provide a minimal reproducible example?