res.json vs res.send vs res.render 의 차이점

YJS·2022년 7월 11일
2

1. res.json

json이 아닌 것도 json 형식으로 바꾸어서 보내줌. Content-type 헤더를 application/JSON으로 고정. 마지막에 res.send()를 호출.

2. res.send

send에 전해진 argument에 따라서 Content-type이 자동적으로 만들어짐.

response를 보내는 역할을 수행. 기본적으로 response 처리를 할때 content-type을 지정해주어야하는데 res.send는 데이터 타입을 파악해서 알맞은 content-type을 지정해줌.

3. res.render

view화면을 랜더링하고 랜더링된 html을 클라이언트에 보내주는 역할.
res.render(view, [, locals] [, callback])

function(req, res){
  res.render('index', { title: 'Express' });
});

-> 서버가 클라이언트에게 index.ejs를 렌더링하여 보내겠다는 의미. ejs파일이 렌더링 되고 html이 된 후에 html 문서를 클라이언트에게 response함.

profile
우당탕탕 개발 일기

0개의 댓글