• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

loggerMiddleware로 morgan처럼 로깅하기 수업에서 @Injectable 부분 에러

22.08.18 18:01 작성 조회수 180

0

LoggerMiddleware class를 작성하던 도중 @Injectable 데코레이터에서 2개의 에러가 납니다.

하나는

Decorator function return type 'ClassDecorator' is not assignable to type 'void | typeof LoggerMiddleware'.

데코레이터 함수 반환 유형 'ClassDecorator'은(는) 'void | typeof LoggerMiddleware' 유형에 할당할 수 없습니다.

라는 에러와

Type 'typeof LoggerMiddleware' has no properties in common with type 'ScopeOptions'.

'typeof LoggerMiddleware' 유형에 'ScopeOptions' 유형과 공통적인 속성이 없습니다.

라는 에러가 납니다

첫번째 에러는 검색하니 주로 데코레이터를 직접 만드신 분들이 내는 에러고 

두번째 에러는 타입스크립트 버전을 올리신 분들이 겪는 에러로 보이는데 어떻게 해결할지 모르겠습니다..

타입스크립트 버전을 내려야 할까요?

현재코드는 이렇습니다.

import {  NestMiddleware, Logger, Injectable } from '@nestjs/common';
import { Request , Response , NextFunction } from 'express';

@Injectable
export class LoggerMiddleware implements NestMiddleware {
   
    private logger = new Logger('HTTP')

    use(req: Request, res: Response, next:NextFunction ) : void {
        const {ip , method , originalUrl } = req ;
        const userAgent  = req.get('user-agent') || '';
        res.on('finish', ()=>{
            const {statusCode} = res;
            const contentLength = res.get("content-length")
            this.logger.log(`${method} ${originalUrl} ${statusCode} ${contentLength} ${userAgent} ${ip}`)
        })
        next()
    }
   
}

 

답변 1

답변을 작성해보세요.

0

현재 타입스크립트 버전 몇버전이신가요?

타입스크립트 4.7버전 한번 써보시겠어요? 제가 확인했을 때는 에러가 없네요.