문제상황
node.js express engine으로 html file을 load할 때 위의 에러가 발생
해결방법
html을 ejs로 rendering한다.
ejs
모듈 설치npm install ejs
app.js
에 라우트 코드를 로딩하는 코드 + 화면 엔진을ejs
로 설정 코드 추가별도 파일에서 라우트 함수를 작성할 때는 express.Router() 함수를 통해 호출
module.exports = router 을 해주면 해당 파일에서 선언한 함수를 router를 통해서 사용 가능
// 화면 engine을 ejs로 설정 app.set('view engine', 'ejs'); app.engine('html', require('ejs').renderFile); //-------------------------- app.use("/", root_router); app.use("/item", item); //@ brief /new_item : 안드로이드에서 작성한 경로 app.use("/user", user); app.use("/noti", noti); app.use("/basket", basket); app.use(express.static(path.join(__dirname, "public")));
views/error.ejs
,views/index.ejs
파일 생성views/error.ejs
<h1><%= message %></h1> <h2><%= error.status %></h2> <pre><%= error.stack %></pre>
views/index.ejs
<!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel='stylesheet' href='../stylesheets/style.css' /> </head> <body> <h1><%= title %></h1> <p>Welcome to <%= title %></p> </body> </html>
참고
'Development > Node.js' 카테고리의 다른 글
Express, mysql2 코드 모듈화하기 (with 트랜잭션) (0) | 2022.08.18 |
---|---|
CodeDeploy 이해하고 푸쉬 서버와 API 서버 분리하기 (0) | 2022.08.18 |
슬랙에서 서버 에러 알림 받고 유연하게 에러 대응하기 (0) | 2022.08.18 |
bcrypt로 비밀번호 암호화 (0) | 2021.11.23 |
Nodejs Webpack으로 빌드하는 과정에서 발생한 에러(ERROR in ./node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js) (0) | 2021.11.23 |
댓글