결국엔 프로그래밍
[node js] 노드는 코드를 모듈로 만들 수 있다 본문
노드는 코드를 모듈로 만들 수 있다. 이 점에서 브라우저의 자바스크립트와 차이가 있다.
모듈
특정한 기능을 하는 함수나 변수들의 집합
모듈은 모듈 자체로도 하나의 프로그램이지만, 다른 프로그램의 부품으로도 사용된다.
3가지 자바스크립트 파일의 예시를 통해 자세히 살펴보자면,
var.js 파일
const odd = '홀수입니다.';
const even = '짝수입니다.';
module.exports = {
odd,
even,
};
func.js 파일
const { odd, even } = require('./var');
function check(num){
if(num%2){
return odd;
}
return even;
}
module.exports = check;
index.js 파일
const { odd, even } = require('./var');
const checkNum = require('./func');
function checkString(str){
if(str.length % 2){
return odd;
}
return even;
}
console.log(chekNum(10));
console.log(checkString('hellojs');
이 예시를 보면,
index.js 는 func의 check 이라는 함수와 var의 odd, even 이라는 변수를 참조한다.
각 func.js와 var.js 파일은 각자의 역할을 수행함과 동시에, index.js의 부품 역할을 한다.
참고문헌: Node.js 교과서 개정2판 (http://www.yes24.com/Product/Goods/62597864)
'자바스크립트(JavaScript) > node js' 카테고리의 다른 글
| [node js] [express] passport 란? (0) | 2021.09.08 |
|---|
Comments