javascript - Migrating Express application to NestJS - Stack Overflow

I have an existing express application, and it is large enough. I want to migrate it to NestJS applicat

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?

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?

Share Improve this question edited Oct 8, 2020 at 9:37 Yaroslav Nekryach asked Oct 7, 2020 at 13:10 Yaroslav NekryachYaroslav Nekryach 1311 silver badge4 bronze badges 3
  • It's probably because your middleware in this example is calling ".end()" which is stopping the request there. Express middleware needs to perform its logic and then call "next()" in order for the request to continue to a route. – nerdy beast Commented Oct 8, 2020 at 15:27
  • It is an example. I don't want the middleware to be called at all. – Yaroslav Nekryach Commented Oct 9, 2020 at 7:41
  • It is not really clear what you want to achieve. If you don't want the globally registered middleware to be called, don't use res.end() use next() but I think that you generally need to restructure your application. Not sure if that is what you are looking for, but checkout the sections about Guards in NestJS documentation? Otherwise please clarify what you are trying to achieve – Mathias Maerker Commented Oct 9, 2020 at 9:50
Add a ment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745202171a4616390.html

相关推荐

  • javascript - Migrating Express application to NestJS - Stack Overflow

    I have an existing express application, and it is large enough. I want to migrate it to NestJS applicat

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信