티스토리 뷰
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
- java
- Collection Framework
- compareTo()
- HackerRank Algorithm
- rest parameter
- equals()
- string class in java
- algorithm
- ... in Javascript
- hackerrank solution
- C++
- 프로그래머스
- HashMap
- Object type casting
- 알고리즘
- code refactoring
- easy algorithm
- Javascript Algorithm
- hackerrank javascript
- hackerrank javascript solution
- math.abs
- spring boot application
- repeat()
- javascript
- easy javascript algorithm
- 프로그래머스 알고리즘
- substring()
- math.max
- 프로그래머스 알고리즘문제
- hackerrank
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 |
30 |
글 보관함