티스토리 뷰
What is a module
A module is an essential single unit of building a complete program. If your code doesn't have modules, it would be difficult to read and run.
How to use module in Node.js?
module.exports is used to return an object from the current module when other modules or programs require it.
To import module, require keyword is used.
Example Code
//module.exports returns the result of requiring call
module.exports.initialize = () => {
return new Promise((resolve, reject) => {
fs.readFile('./data/employees.json', (err, data) => {
if(err){
reject("unable to read file");
}
else{
employees = JSON.parse(data);
fs.readFile('./data/departments.json', (err, data) => {
if(err){
reject("unable to read file");
}
else{
departments = JSON.parse(data);
resolve();
}
});
}
});
});
}
|
Reference
https://www.sitepoint.com/understanding-module-exports-exports-node-js/
'Node.js' 카테고리의 다른 글
Upgrade node.js version (0) | 2021.01.23 |
---|---|
Using bcrypt inside async (0) | 2020.09.25 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- hackerrank solution
- java
- C++
- substring()
- Javascript Algorithm
- 프로그래머스 알고리즘문제
- hackerrank
- equals()
- math.abs
- Object type casting
- easy javascript algorithm
- 프로그래머스
- compareTo()
- spring boot application
- code refactoring
- rest parameter
- string class in java
- 알고리즘
- math.max
- HashMap
- HackerRank Algorithm
- algorithm
- easy algorithm
- javascript
- repeat()
- hackerrank javascript solution
- 프로그래머스 알고리즘
- hackerrank javascript
- ... in Javascript
- Collection Framework
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함