내일배움캠프 TIL (230104)

Jiumn·2023년 1월 5일
0

ejs 렌더링 에러

Error: Failed to lookup view "error" in views directory

ejs 파일을 views 폴더에 넣고 렌더링하려고 하는데 다음과 같은 에러가 발생했다.

Error: Failed to lookup view "error" in views directory "C:\Users\82102\Documents\GGG\views"
    at Function.render (C:\Users\82102\Documents\GGG\node_modules\express\lib\application.js:597:17)
    at ServerResponse.render (C:\Users\82102\Documents\GGG\node_modules\express\lib\response.js:1039:7)
    at C:\Users\82102\Documents\GGG\app.js:66:7
    at Layer.handle_error (C:\Users\82102\Documents\GGG\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (C:\Users\82102\Documents\GGG\node_modules\express\lib\router\index.js:326:13)
    at C:\Users\82102\Documents\GGG\node_modules\express\lib\router\index.js:286:9
    at Function.process_params (C:\Users\82102\Documents\GGG\node_modules\express\lib\router\index.js:346:12)
    at next (C:\Users\82102\Documents\GGG\node_modules\express\lib\router\index.js:280:10)
    at C:\Users\82102\Documents\GGG\app.js:60:3
    at Layer.handle [as handle_request] (C:\Users\82102\Documents\GGG\node_modules\express\lib\router\layer.js:95:5)
Error: Failed to lookup view "error" in views directory

views 경로 지정이 잘못 되어 있다는 의미로 해석되는데 경로를 잘 지정해둔 상태였기 때문에 원인을 알 수 없었다.

해결방법

결론적으로는 ejs 문법이 잘못 적용돼서 생긴 에러였다.

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <body>
      <h1><%= data.image %></h1>
      <p>전화번호 <%= data.phonenumber %></p>
      <p>주소 <%= data.address %></p>
      <p>이미지 <img src="/upload/<%= data.image %>" width="300" /></p>
      <p>요청사항 <%= data.comment %></p>
    </body>
  </head>
</html>

코드를 보면 <%= data.image %>와 같이 앞서 라우터에서 받은 data 객체에서 출력하고 싶은 변수를 추출해야 한다. (예. data.phonenumber)

profile
Back-End Wep Developer. 꾸준함이 능력이다.

0개의 댓글