강의

멘토링

커뮤니티

Inflearn Community Q&A

neok's profile image
neok

asked

Node.js Textbook - From Basics to Project Practice

Router Refactoring

matchedUrl 정의할 때

Written on

·

295

0

const matchedUrl = router[req.method][req.url]

(matchedUrl || router[req.method]['*'])(req, res);

보통 객체에서 키 값을 불러올 때는 . 을 사용하잖아요.

[]는 보통 배열에서 사용되고?

왜 저런식으로 사용됐는지 이해가 잘 안됩니다.

또 matchedUrl 정의가 (req, res) 까지인가요?

그렇다면 const 정의할 때 옵션을 정해준거랑 비슷한건가요?

javascriptmongodbmysqlnodejs

Answer 2

1

zerocho님의 프로필 이미지
zerocho
Instructor

키가 변수일 경우에는 객체에서도 []를 씁니다. 객체[변수] 이런 식으로요.

따라서 router[req.method][req.url]는 객체 안 속성을 찾아가는 거고요.

(matchedUrl || router[req.method][''])(req, res) 이 부분은 함수를 (req, res)로 호출하는 것입니다.

matchedUrl이라는 함수가 있으면 matchedUrl(req,res)를 하는 것이고요, 없다면 router[req.method][](req, res)하는 것입니다.

0

neok님의 프로필 이미지
neok
Questioner

감사합니다! 이해했어요!

neok's profile image
neok

asked

Ask a question