Migrating Express application to NestJS

故事扮演 提交于 2021-01-27 20:30:13

问题


I have an existing express application, and it is large enough. I want to migrate it to NestJS application, but do it step by step. So I have created Nest app with an existed Express instance, by faced a problem - all nest handlers always add after Express handlers. But I have a root Express middleware I want to apply only for some handlers that go after it. How can I apply Nest handlers before Express handlers?

For example. I created a new NestJS project, and changed 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();

On the root path I expect to see Hello World! but I see Auth middleware

I understand that I can do something like

expressApp.get('/test', authMiddleware, ((req, res) => res.end('Test')))

But current project is too large with sub routers and not only one middleware. Maybe there is another way to do it right?


回答1:


Your auth middleware is always ending the request and not behaving the way middleware usually does. Typically middleware has a signature like this...

const authMiddleware = (req, res, next) => {
  // do auth things like verify a JWT and attach it to req.user

  next()
}
expressApp.use(authMiddleware)

const loggingMiddleware = (req, res, next) => {
  const { headers, body, user, route } = req
  console.log({ headers, body, user, route })
  next()
}
expressApp.use(loggingMiddleware)

In the above code, the authMiddleware ALWAYS executes before the loggingMiddleware because it was attached first. If, say, we reversed the order and attached loggingMiddleware first, req.user would always be undefined. The Express documentation is very descriptive in how the middleware control flow operates, and NestJS follows the same patterns under the hood.



来源:https://stackoverflow.com/questions/64244716/migrating-express-application-to-nestjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!