Node.js modules
๐จ๏ธ ChildProcess
โ ChildProcess
๊ฐ๋
- Node.js ํ๋ก์ธ์ค ๋ด์์ ๋ค๋ฅธ ํ๋ก์ธ์ค๋ฅผ ์คํํ๊ณ ๊ทธ์ ์ํธ ์์ฉํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ชจ๋
- Node.js์์ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ ๊ณต
- exec(), spawn(),fork() ๋ฑ์ ํจ์๋ฅผ ์ ๊ณต
- exec()
- ํจ์๋ ์ ธ ๋ช ๋ น์ด๋ฅผ ์คํํ๊ณ ํด๋น ๊ฒฐ๊ณผ๋ฅผ ๋ฒํผ๋ง
- spawn()
- ์๋ก์ด ํ๋ก์ธ์ค๋ฅผ ๋ง๋ค๊ณ ๊ทธ ํ๋ก์ธ์ค์์ ํ์ค ์ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ ๊ณต
- fork()
- spawn() ํจ์์ ์ ์ฌํ์ง๋ง, ๋ถ๋ชจ ํ๋ก์ธ์ค์ ์์ ํ๋ก์ธ์ค ๊ฐ์ ํต์ ํ ์ ์๋ IPC ์ฑ๋์ ์๋์ผ๋ก ์ค์
- exec()
- ChildProcess ๋ชจ๋์ ์ฌ์ฉํ๋ฉด Node.js ํ๋ก์ธ์ค ๋ด์์ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ์คํํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ์์ฌ ์ ์์
- Node.js ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ธ๋ถ ๋ช ๋ น์ด ์คํ, ์์ ํ๋ก์ธ์ค ์์ฑ, ํด๋ฌ์คํฐ๋ง ๋ฑ ๋ค์ํ ์ฉ๋๋ก ํ์ฉ ๊ฐ๋ฅ
์ฌ์ฉ์์
Electorn์์ ChildProcess๋ฅผ ์ฌ์ฉํด ์ธ๋ถ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ ์์
const { exec } = require('child_process');
// 'ls' ๋ช
๋ น์ด ์คํ
exec('ls', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
ChildProcess๋ชจ๋์ ์ฌ์ฉํ ๋ฉ์ธ ํ๋ก์ธ์ค์ ๋ ๋๋ฌ ํ๋ก์ธ์ค ๊ฐ์ ํต์ ์์
const { fork } = require('child_process');
// ๋๋๋ฌ ํ๋ก์ธ์ค์์ ์คํํ ๋ชจ๋ ๊ฒฝ๋ก
const pathToScript = path.join(__dirname, 'myScript.js');
// ๋๋๋ฌ ํ๋ก์ธ์ค์์ myScript.js ๋ชจ๋ ์คํ
const child = fork(pathToScript);
// ๋๋๋ฌ ํ๋ก์ธ์ค๋ก ๋ฉ์์ง ๋ณด๋ด๊ธฐ
child.send('hello from main process!');
// ๋๋๋ฌ ํ๋ก์ธ์ค์์ ๋ฉ์์ง ๋ฐ๊ธฐ
child.on('message', (msg) => {
console.log(`received message from renderer process: ${msg}`);
});
child_process ๋ชจ๋์ spawn() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ Windows ์ด์์ฒด์ ์์ cmd.exe๋ฅผ ์คํํ๊ณ ํด๋น ํ๋ก์ธ์ค์ ์ํธ์์ฉํ๋ ์์
const { spawn } = require('child_process');
/*
* ์๋ก์ด cmd.exe ์์ ์ด๊ณ 'dir' ๋ช
๋ น์ด๋ฅผ ์คํํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅ
*/
// Windows ์ด์์ฒด์ ์์๋ 'cmd.exe'๋ฅผ ์ฌ์ฉํ์ฌ ๋ช
๋ น์ด ์คํ
const cmd = spawn('cmd.exe');
cmd.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
cmd.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
cmd.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
// ๋ช
๋ น์ด ์
๋ ฅ์ ์ํด stdin์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ก
cmd.stdin.write('dir\\n'); // ๋ช
๋ น์ด ์ ์ก
cmd.stdin.end(); // ์
๋ ฅ ์ข
๋ฃ
๐จ๏ธ child_process.spawn(command[, args][, options])
โ child_process.spawn(command[, args][, options])
๊ฐ๋
- command
- ์คํํ ๋ช ๋ น
- args
- ๋ฌธ์์ด ์ธ์ ๋ชฉ๋ก
- options
- cwd ์์ ํ๋ก์ธ์ค์ ํ์ฌ ์์ ๋๋ ํ ๋ฆฌ
- shell true์ผ ๋ ์ ๋ด๋ถ์์ ์คํ.
๐จ๏ธ ChildProcessWithoutNullStreams
โ ChildProcessWithoutNullStreams
๊ฐ๋
- Node.js์ child_process ๋ชจ๋์์ ์ ๊ณตํ๋ ํด๋์ค ์ค ํ๋
- ChildProcess ํด๋์ค์ ๋ฌ๋ฆฌ, ํ์ค ์
๋ ฅ ์คํธ๋ฆผ๊ณผ ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ์ด null์ด ์๋๋ผ๋ฉด ChildProcessWithoutNullStreams ๊ฐ์ฒด๋ฅผ ๋ฐํ
- ๊ธฐ๋ณธ์ ์ผ๋ก ChildProcess ํด๋์ค์์๋ ํ์ค ์ ๋ ฅ ์คํธ๋ฆผ๊ณผ ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ์ด ๋ชจ๋ null๋ก ์ค์
- ChildProcessWithoutNullStreams์ ๊ฒฝ์ฐ ์ด๋ฌํ ์ ํ์ ์์ ๊ณ , ํ์ค ์ ๋ ฅ ์คํธ๋ฆผ๊ณผ ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ์ ํตํด ํ๋ก์ธ์ค์ ์ํธ์์ฉํ ์ ์์
- ChildProcess ํด๋์ค์ ๊ธฐ๋ฅ์ ํ์ฅํ์ฌ ํ๋ก์ธ์ค์ ๋์ฑ ํจ์จ์ ์ผ๋ก ์ํธ์์ฉํ ์ ์๋๋ก ํด์ฃผ๋ ํด๋์ค
์ฌ์ฉ์์
const { spawn } = require('child_process');
//stdio: 'inherit' ์ต์
์ ์ถ๊ฐํ์ฌ, ๋ถ๋ชจ ํ๋ก์ธ์ค์ ํ์ค ์
๋ ฅ ์คํธ๋ฆผ,
//ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ ๋ฐ ํ์ค ์ค๋ฅ ์คํธ๋ฆผ์ ์์ ํ๋ก์ธ์ค๋ก ์ ๋ฌ
//ChildProcessWithoutNullStreams ๊ฐ์ฒด๋ฅผ ๋ฐํ
const ls = spawn('ls', ['-lh', '/usr'], { stdio: 'inherit' });
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
๐จ๏ธ stdout, stderr
โ stdout, stderr
๊ฐ๋
stdout
- "standard output"์ ์ฝ์
- ํ๋ก๊ทธ๋จ์ด ์คํ๋ ๋ ์ถ๋ ฅํ๋ ๊ฒฐ๊ณผ๋ฌผ์ด๋ ๋ฉ์์ง
- ์ฝ์(Console)์ด๋ ํฐ๋ฏธ๋(Terminal)์ ์ถ๋ ฅ
- ํ๋ก๊ทธ๋๋ฐ์์๋ stdout์ ํ์ค ์ถ๋ ฅ ์คํธ๋ฆผ(Standard output stream)์ด๋ผ๊ณ ๋ ๋ถ๋ฆ
stderr
- "standard error"์ ์ฝ์
- ํ๋ก๊ทธ๋จ ์คํ ์ค ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ
- ์ฝ์์ด๋ ํฐ๋ฏธ๋์์ ๋นจ๊ฐ์์ผ๋ก ์ถ๋ ฅ
- ํ๋ก๊ทธ๋๋ฐ์์๋ stderr์ ํ์ค ์ค๋ฅ ์คํธ๋ฆผ(Standard error stream)์ด๋ผ๊ณ ๋ ๋ถ๋ฆ
์ฌ์ฉ ์์
์์ ํ๋ก์ธ์ค์ stdout๊ณผ stderr์ ๊ฐ๊ฐ data ์ด๋ฒคํธ์ error ์ด๋ฒคํธ๋ก ์บก์ฒ
/*
* Node.js์ child_process ๋ชจ๋์์๋ spawn() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ
*์์ ํ๋ก์ธ์ค๋ฅผ ์คํํ ๋,
* ์ด๋ฌํ stdout๊ณผ stderr์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ํตํด ์บก์ฒ
*/
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
๐จ๏ธ fs
โ fs
๊ฐ๋
- Node.js์์ ํ์ผ ์์คํ ์์ ์ ์ํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ชจ๋
- ํ์ผ ์์ฑ, ์ฝ๊ธฐ, ์ฐ๊ธฐ, ์์ , ์ญ์ ๋ฑ์ ํ์ผ ์์คํ ์์ ์ ์ํํ๋ ํจ์๋ค์ ์ ๊ณต
- Node.js์ ๊ธฐ๋ณธ ๋ชจ๋๋ก ์ ๊ณต๋๋ฉฐ, ๋ณ๋์ ์ค์น ์์ด ์ฌ์ฉํ ์ ์์
- fs ๋ชจ๋์์ ์ ๊ณตํ๋ ํจ์๋ค์ ๋๋ถ๋ถ ๋น๋๊ธฐ์ ์ผ๋ก ๋์, ๋๊ธฐ์ ์ผ๋ก ๋์ํ๋ ํจ์๋ ์ ๊ณต
- ์ฝ๋ฐฑ ํจ์๋ฅผ ํตํด ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
์ฃผ์ ํจ์
* `fs.readFile()`: ํ์ผ์ ๋น๋๊ธฐ์ ์ผ๋ก ์ฝ์ด๋ค์
๋๋ค.
* `fs.readFileSync()`: ํ์ผ์ ๋๊ธฐ์ ์ผ๋ก ์ฝ์ด๋ค์
๋๋ค.
* `fs.writeFile()`: ํ์ผ์ ๋น๋๊ธฐ์ ์ผ๋ก ์๋๋ค.
* `fs.writeFileSync()`: ํ์ผ์ ๋๊ธฐ์ ์ผ๋ก ์๋๋ค.
* `fs.appendFile()`: ํ์ผ์ ๋์ ๋ด์ฉ์ ์ถ๊ฐํฉ๋๋ค.
* `fs.unlink()`: ํ์ผ์ ์ญ์ ํฉ๋๋ค.
์ฌ์ฉ ์์
const fs = require('fs');
const path = require('path');
const { dialog } = require('electron').remote;
// ํ์ผ ๋ค์ด์ผ๋ก๊ทธ๋ฅผ ์ด์ด์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์
๋ ฅ๋ฐ์ต๋๋ค.
dialog.showOpenDialog({
properties: ['openFile']
}).then(result => {
if (!result.canceled) {
// ์ ํํ ํ์ผ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
const filePath = result.filePaths[0];
// ํ์ผ์ ์ฝ์ด๋ค์
๋๋ค.
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) {
console.error(err);
return;
}
// ํ์ผ ๋ด์ฉ์์ ๋จ์ด ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
const wordCount = data.split(/\\s+/).length;
// ํ๋ฉด์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
document.getElementById('result').textContent = `๋จ์ด ์: ${wordCount}`;
});
}
}).catch(err => {
console.error(err);
});
๐จ๏ธ path
โ path
๊ฐ๋
- ํ์ผ ๊ฒฝ๋ก์ ๊ด๋ จ๋ ์ ํธ๋ฆฌํฐ ํจ์๋ฅผ ์ ๊ณต
- ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ ํ๊ฒ ์กฐ์ํ๊ณ , ํ์ผ ๊ฒฝ๋ก์ ๊ตฌ์ฑ ์์๋ฅผ ์ถ์ถํ๊ฑฐ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅ
์ฌ์ฉ ์์
path ๋ชจ๋์ ์ฌ์ฉํ์ฌ /path/to/some/file.txt ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ฒ๋ฆฌ
const path = require('path');
const fullPath = '/path/to/some/file.txt';
// ํ์ผ ๊ฒฝ๋ก์์ ํ์ผ ์ด๋ฆ์ ์ถ์ถํฉ๋๋ค.
const filename = path.basename(fullPath);
console.log('Filename:', filename);
// ํ์ผ ๊ฒฝ๋ก์์ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ฅผ ์ถ์ถํฉ๋๋ค.
const directory = path.dirname(fullPath);
console.log('Directory:', directory);
// ํ์ผ ๊ฒฝ๋ก์์ ํ์ฅ์๋ฅผ ์ถ์ถํฉ๋๋ค.
const extname = path.extname(fullPath);
console.log('Extension:', extname);
// ํ์ผ ๊ฒฝ๋ก๋ฅผ ์กฐํฉํฉ๋๋ค.
const combinedPath = path.join(directory, 'newfile.md');
console.log('Combined Path:', combinedPath);
// ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ ๋ ๊ฒฝ๋ก๋ก ๋ณํํฉ๋๋ค.
const absolutePath = path.resolve(combinedPath);
console.log('Absolute Path:', absolutePath);
/*
๊ฒฐ๊ณผ๊ฐ
Filename: file.txt
Directory: /path/to/some
Extension: .txt
Combined Path: \\path\\to\\some\\newfile.md
Absolute Path: C:\\path\\to\\some\\newfile.md
*/
๐จ๏ธ yargs
โ yargs
์ค์น
npm i yargs
๊ฐ๋
- ๋ช ๋ นํ ์ธ์(command line arguments)๋ฅผ ํ์ฑ(parsing)ํ๊ธฐ ์ํ ์ ํธ๋ฆฌํฐ
- ๋ช ๋ นํ ์ธ์๋ฅผ ํ์ฑํ์ฌ ๊ฐ์ฒด ํํ๋ก ๋ฐํํ๋ฏ๋ก, ์ธ์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ฝ๊ฒ ํด์ค
- ๋ช ๋ นํ ์ธ์๋ฅผ ์ฌ์ฉ์ ์นํ์ ์ธ ํํ๋ก ์ ์ํ ์ ์๋ ๊ธฐ๋ฅ๋ ์ ๊ณต
์ฌ์ฉ ์์
const yargs = require('yargs');
// ์ฌ์ฉ์ ์นํ์ ์ธ ํํ๋ก ๋ช
๋ นํ ์ธ์๋ฅผ ์ ์ํฉ๋๋ค.
const argv = yargs
.usage('Usage: $0 [options] <path>')
.option('r', {
alias: 'recursive',
describe: 'Recursively process all files in the directory',
type: 'boolean',
default: false
})
.option('e', {
alias: 'extension',
describe: 'Specify the file extension to process',
type: 'string',
default: '.txt'
})
.help('h')
.alias('h', 'help')
.argv;
// ๋ช
๋ นํ ์ธ์๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
console.log('Options:', argv);
// path ์ธ์๋ฅผ ์ถ์ถํฉ๋๋ค.
const path = argv._[0];
console.log('Path:', path);
๐จ๏ธ yargs ์ argv
โ argv
๊ฐ๋
- argv๋ ์ฌ์ฉ์๊ฐ ๋ช ๋ นํ ์ธ์(command line argument)๋ฅผ ์ ๋ฌํ ๋, ์ด๋ฅผ ํ์ฑ(parsing)ํ ๊ฒฐ๊ณผ๋ฅผ ๋ด๊ณ ์๋ ๊ฐ์ฒด
- argv ๊ฐ์ฒด๋ ๋ค์ํ ํ๋กํผํฐ๋ฅผ ๊ฐ๊ณ ์์ผ๋ฉฐ, ์ฌ์ฉ์๊ฐ ์ ๋ฌํ ๋ช ๋ นํ ์ธ์์ ์ด์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์
์ฌ์ฉ ์์
{
name: 'Iseo',
age: 29,
_: [],
'$0': 'memo.js'
}
const yargs = require('yargs');
const argv = yargs
.option('name', {
alias: 'n',
describe: 'User name',
type: 'string'
})
.option('age', {
alias: 'a',
describe: 'User age',
type: 'number'
})
.argv;
console.log(argv);
{
name: 'Iseo',
n: 'Iseo',
age: 29,
a: 29,
_: [],
'$0': 'memo.js'
}
๐จ๏ธ properties-reader
โ properties-reader
๊ฐ๋
- Node.js์์ ํ๋กํผํฐ ํ์ผ(.properties)์ ์ฝ์ด์์ JavaScript ๊ฐ์ฒด๋ก ๋ณํํด์ฃผ๋ ๋๊ตฌ
- ๋ณ๋์ JSON ํ์ผ์ ์ฌ์ฉํ์ง ์๊ณ ๋, ํ๋กํผํฐ ํ์ผ์ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ์ค์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌ
- Electron ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ค์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐ์ ์ ์ฉํ๊ฒ ์ฌ์ฉ
์ฌ์ฉ ์์
# config.properties
host=localhost
port=8080
database=mydb
const PropertiesReader = require('properties-reader');
// config.properties ํ์ผ์ ์ฝ์ด์ PropertiesReader๊ฐ์ฒด๋ฅผ ์์ฑ
//.path() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๊ฐ์ฒด๋ก ๋ณํํ๊ณ ๊ฐ ํ๋กํผํฐ์ ์ ๊ทผ ๊ฐ๋ฅ
const properties = PropertiesReader('config.properties').path();
console.log(properties.host); // localhost
console.log(properties.port); // 8080
console.log(properties.database); // mydb
๐จ๏ธ fs.existsSync()
โ fs.existSync()
๊ฐ๋
- ์ฃผ์ด์ง ๊ฒฝ๋ก์ ํ์ผ์ด๋ ๋๋ ํ ๋ฆฌ๊ฐ ์กด์ฌํ๋์ง ํ์ธ
- ๋๊ธฐ์ ์ผ๋ก ์๋ํ๋ฉฐ, ์ฃผ์ด์ง ๊ฒฝ๋ก๊ฐ ์กด์ฌํ๋ฉด **true**๋ฅผ ๋ฐํํ๊ณ , ์กด์ฌํ์ง ์์ผ๋ฉด **false**๋ฅผ ๋ฐํ
์ฌ์ฉ ์์
const fs = require('fs');
if (fs.existsSync('/path/to/file')) {
console.log('File exists');
} else {
console.log('File does not exist');
}
๐จ๏ธBuffer
โ Buffer
๊ฐ๋
- Node.js์ **Buffer**ํด๋์ค๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ฐ ์ฌ์ฉ๋๋ ์ผ์ข ์ ๋ฐฐ์ด
- ๋ฉ๋ชจ๋ฆฌ ์์์ ์ผ์ ํ ํฌ๊ธฐ์ ๊ณต๊ฐ์ ํ๋ณดํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ฉฐ, ์ด๋ฅผ ํตํด ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์กฐ์ํ๊ณ ๋ค๋ฅธ ์์คํ ๊ณผ ์ํธ์์ฉ
- **Buffer**๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ฐ ์ ์ฉํ ๋ฉ์๋์ ์์ฑ์ ์ ๊ณต
- **Buffer**ํด๋์ค๋ Node.js์ I/O ์์ ์์ ๋ง์ด ์ฌ์ฉ
์ฌ์ฉ ์์
- ๋ฌธ์์ด์ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ก ๋ณํ
const str = 'Hello, world!';
const buf = Buffer.from(str, 'utf8');
console.log(buf); // <Buffer 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21>
- ๊ณ ์ ํฌ๊ธฐ์ **Buffer**์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ์์
const buf1 = Buffer.alloc(10);
const buf2 = Buffer.allocUnsafe(10);
console.log(buf1); // <Buffer 00 00 00 00 00 00 00 00 00 00>
console.log(buf2); // <Buffer 58 0f 70 08 30 00 00 00 00 00>
- ํ์ผ์ ์ฝ๊ณ ์ฐ๋ ์์
const fs = require('fs');
// ํ์ผ์ ์ฝ์ด๋ค์
๋๋ค.
fs.readFile('input.txt', (err, data) => {
if (err) throw err;
// ์ฝ์ด๋ค์ธ ๋ฐ์ดํฐ๋ฅผ `Buffer` ์ธ์คํด์ค๋ก ๋ณํํฉ๋๋ค.
const buf = Buffer.from(data);
// `Buffer` ์ธ์คํด์ค๋ฅผ ์ฝ์์ ์ถ๋ ฅํฉ๋๋ค.
console.log(buf);
// ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์ฐ๊ธฐ ์ํ `Buffer` ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค.
const writeBuf = Buffer.from('Write this data to file', 'utf8');
// ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์๋๋ค.
fs.writeFile('output.txt', writeBuf, (err) => {
if (err) throw err;
console.log('Data written to file');
});
});
๋๊ธ