我有一个现有的 Express 应用程序,并且它足够大。我想将其迁移到 NestJS 应用程序,但要一步一步地进行。因此,我使用现有的 Express 实例创建了 Nest 应用程序,但遇到了一个问题 - 所有嵌套处理程序总是在 Express 处理程序之后添加。但我有一个根 Express 中间件,我只想应用于它后面的一些处理程序。如何在 Express 处理程序之前应用 Nest 处理程序?
例如。我创建了一个新的 NestJS 项目,并更改了 main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as express from 'express';
const expressApp = express();
const authMiddleware = (req, res) => res.end('Auth middleware');
expressApp.use(authMiddleware);
expressApp.get('/test', ((req, res) => res.end('Test')))
async function bootstrap() {
const app = await NestFactory.create(AppModule, new ExpressAdapter(expressApp));
await app.listen(3000);
}
bootstrap(); …Run Code Online (Sandbox Code Playgroud)