在我的 NestJs 中间件设置中没有收到任何请求信息

Not receiving any request information in my NestJs Middleware set up

我有一个 NestJs 和 Angular 应用程序。获得 "production" 版本 运行 花了一些时间,我最终能够在我的 main.ts 服务器文件中使用这样的设置来弄清楚它:

app.use('*', (req, res) => {
    res.sendFile(CLIENT_FILES + '/index.html');
});

现在正在尝试移动到已实现的中间件:

我一直在使用 Bo 的示例来实现中间件:https://medium.com/@bo.vandersteene/use-nest-as-your-server-side-application-with-an-angular-frontend-540b0365bfa3

我的中间件在每次请求时都会受到影响,但我无法从 Request 参数中获取任何信息,因此什么也没有发生,应用程序本身挂起。我不确定我错过了哪一步,或者我是否有一个现有的设置阻止了对请求对象的可见性。非常感谢任何方向

下面是我的中间件日志:

结果 大请求获取路径:/ da req get originalUrl : / da req 获取主机名:localhost 请求获取 baseUrl :
请求获取 url: /

我的frontendmiddleware.ts:

import { NestMiddleware, Injectable } from "@nestjs/common";
import { join } from 'path';
import { Request, Response } from 'express';

const allowedExt = [
    '.js',
    '.ico',
    '.css',
    '.png',
    '.jpg',
    '.woff2',
    '.woff',
    '.ttf',
    '.svg',
  ];

  const ROUTE_PREFIX = 'api';
  const resolvePath = (file: string) => join(__dirname, '..', file);


  @Injectable()
  export class FrontendMiddleware implements NestMiddleware {

    use(req: Request, res: Response, next: Function) {

       console.log('da req : ' + req );
       console.log( ' da req get path :  ' + req.path);
       console.log( ' da req get originalUrl :  ' + req.originalUrl);
       console.log( ' da req get hostname :  ' + req.hostname);
       console.log( ' da req get baseUrl :  ' + req.baseUrl);
   console.log( ' da req get url:  ' + req.url );

        const { url } = req; 

        if (url.indexOf(ROUTE_PREFIX) === 1) {
          // it starts with /api --> continue with execution
          next();
        } else if (allowedExt.filter(ext => url.indexOf(ext) > 0).length > 0) {
          // it has a file extension --> resolve the filea
          console.log('here be the other files: ' + resolvePath(url));
          res.sendFile(resolvePath(url));
        } else {
          // in all other cases, redirect to the index.html!
          res.sendFile(resolvePath('index.html'));
        }
    }


  }

我的app.module.ts

import { Module, NestModule, RequestMethod, MiddlewareConsumer } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SharedModule } from './shared/shared.module';
import { ConfigurationService } from './shared/configuration/configuration.service';
import { Configuration } from './shared/configuration/configuration.enum';
import { UserModule } from './user/user.module';
import { FrontendMiddleware } from './middleware/frontend.middleware';

@Module({
  imports: [
    SharedModule,
    MongooseModule.forRoot(ConfigurationService.connectionString),
    UserModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {

  constructor(private readonly configurationService: ConfigurationService) {
    AppModule.port = AppModule.normalizePort(configurationService.get(Configuration.PORT));
    AppModule.host = configurationService.get(Configuration.HOST);
    AppModule.isDev = configurationService.isDevelopment;

    console.log('port : ' + AppModule.port);
    console.log('host : ' + AppModule.host);
    console.log('isDev : ' + AppModule.isDev);

  }

  configure(consumer: MiddlewareConsumer): void {
    consumer.apply(FrontendMiddleware).forRoutes(
        {
          path: '/**', // For all routes
          method: RequestMethod.ALL, // For all methods
        },
      );
  }

  static host: string;
  static port: number | string;
  static isDev: boolean;


  private static normalizePort(param: number | string): number | string {
    const portNumber: number = typeof param === 'string' ? parseInt(param, 10) : param;
    if (isNaN(portNumber)) {
      return param;
    } else if (portNumber >= 0) {
      return portNumber;
    }
  }
}

我的新 main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './shared/filters/http-exception.filter';
import { join } from 'path';

declare const module: any;

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  if (module.hot) {
    module.hot.accept();
    module.hot.display(() => app.close());
  }

  app.useGlobalFilters(new HttpExceptionFilter());
  app.enableCors();

  await app.listen(AppModule.port);
}
bootstrap();

更新——在通过文档工作时,我能够使 "functional middleware" 正常工作,但仍然没有使用中间件 class ( https://docs.nestjs.com/middleware)

export function logger(req, res, next) {
  console.log(`Request...`);
  next();
};

从路径中删除两个星号似乎有所不同。我认为我的设置导致了这些结果,但暂时似乎可以做到:

路径:'/'