nodejs

<nodejs>내가 모르는 것들 정리

빛나는구슬 2023. 11. 2. 23:53

nodejs를 배우면서

내가 까먹거나 모르는 개념, 아주 기초적인 거라도 정리하자 !

 

const profile = {
	name:"쥬쥬",
    age:25,
    school:"쥬라기대학교",
    age:27
    }
    
    console.log(profile.age)

→ 콘솔에 27 출력

(밑에있는 age가 위의 age를 덮어 씀)

 


 

class ProductController {
productService;

constructor(private readonly productService){
this.productService = productService;
}

buyProduct(req,res){
this.productService.qqq();
};
}
public, private, protected, readonly 중 1개라도 있으면

두번째 줄의 productService와

다섯번째줄의 this.productService = productService 생략 가능 (자동으로 입력됨)

 


const productService = new ProductService()

const productController = new ProductController(productService)
app.post("/products/buy", productController.buyProduct)

위 express에서는 ~new ProductController(productService)

밑줄친것과 같이 의존성 주입을 할 수 있음

 

그렇다면 nestjs에서는 ?

@Module({
imports:[],
controllers: [ProductController],
providers:[ProductService],
})
export class ProductModule{}

providers에 의존성 주입 가능

 

 


interface IProfile {
name:string
age:number
job:string
phoneNum?:string
}

위 타입에서 name,age,job만 있는 타입을 만들고 싶다면

→ Omit<IProfile, "phoneNum">해주면 phoneNum을 제외한 타입이 만들어짐(효율적)