nestjs의 middleware는 express의 middleware와 동일함
request, response objects가 있고 next() 함수 사용
Ex)
import { Request, Response, NextFunction } from "express";
export function Middleware(
req: Request,
res: Response,
next: NextFunction
) {
console.log("middleware");
next();
}
import { NestMiddleware } from "@nestjs/common";
import { NextFunction, Request, Response } from "express";
class Middleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
console.log("middleware");
next();
}
}
Ex1) app.module.ts
export class AppModule {
configure(consumer: MiddlewareConsemer) {
consumer.apply(jwtMiddleware).forRoute({
path: "*",
method: RequestMethod.ALL,
});
}
}
Ex2) main.ts
app.use(사용할 미들웨어)