모듈화

이영광·2021년 9월 9일
0

Node.js

목록 보기
2/10

사실 아직 모듈화라는것이 정확하게 어떻게 해야되는것일까라는 의미를 몰랐었다.

이번 과정을 해보면서 모듈화가 무엇인지 확실하게 알게됬다

/modules/lib/fortune.js

const fortuneCookies = [
  "Conquer your fears or they will conquer you.",
  "Rivers need springs.",
  "Do not fear what you don't know.",
  "You will have a pleasant surprise.",
  "Whenever possible, keep it simple.",
];

exports.geFortune = () => {
  const idx = Math.floor(Math.random() * fortuneCookies.length);
  return fortuneCookies[idx];
};

main.js 에서 쓰고있던것을 따로 때가지고 함수로 만들고 익스포트 시켰다

이것을 main.js에서 원래 있던 내용을 삭제해주고 리콰이어(임포트)해서 가져오기로하니

app.get("/about", (req, res) => {
  res.render("about", { fortune: fortune.getFortune() });
});

어바웃경로로 설정후 핸들바에서 설정한데로 연결해주었다

그럼 훨씬 깔끔하게 정리가되서 보기가 편하다

모듈화를 해두면 항상 어디서든지 꺼내쓸수있는것이다

라이버러리 형식이란 이런것을 의미한다는것을 느낀다

profile
《REACT》《JAVASCRIPT 》 만지고있어욤

0개의 댓글