小编Yar*_*ach的帖子

将 Express 应用程序迁移到 NestJS

我有一个现有的 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)

javascript migration node.js express nestjs

11
推荐指数
1
解决办法
8661
查看次数

标签 统计

express ×1

javascript ×1

migration ×1

nestjs ×1

node.js ×1