본문 바로가기
WEB/Nodejs

[Nodejs] Module

by Ellen571 2020. 8. 4.

[생활코딩] Node.js - 모듈의 형식

 

 

Module

- 객체가 많아지면 정리정돈할 필요가 생김

- 정리된 객체을 파일로 만든 것이 모듈

 

모듈 내보내기

 

var M = {
	v: 'v',
    	f: function(){ console.log(this.v); }
}

module.exports = M; // 모듈 내보내기

또는

module.exports = {
	v: 'v',
    	f: function(){ console.log(this.v); }
}

 

모듈 사용하기

 

var part = require('./mpart.js'); // 모듈 가져오기
console.log(part); // {v: 'v', f: [Function: f]}
part.f(); // v
반응형

'WEB > Nodejs' 카테고리의 다른 글

[Nodejs] API  (0) 2020.08.05
[Nodejs] 보안 - path.parse(), sanitize-html  (0) 2020.08.05
[Nodejs] 객체지향 프로그래밍  (0) 2020.08.04
[Nodejs] Function  (0) 2020.08.04
[Nodejs] Array/Object  (0) 2020.08.04