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