반응형
05-14 20:55
Today
Total
«   2024/05   »
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 31
관리 메뉴

개발하는 고라니

[Node.js] 파일 쓰기 - Console 본문

Framework/Node.js

[Node.js] 파일 쓰기 - Console

조용한고라니 2021. 5. 4. 12:34
반응형

콘솔에 문자열을 입력하고 그 내용을 파일에 저장하는 예제를 해보고자 한다. 자바스크립트는 콘솔 입력이 없기에 Node.js를 사용해야한다.

 

const fs = require('fs');
const readline = require('readline');

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var data = '';

rl.on('line', (input) => {
    data += input + '\n';
});
rl.on('close', () => {
    fs.appendFile('./test.txt', data, 'utf8', (err) => {
        if(err) throw err;

        console.log('파일 작성 완료');
    });
});

fs는 'File System'의 약자로 노드에서 파일을 다루기 위한 내장 모듈이고, readline 또한 내장모듈인데, 이는 콘솔에서의 입출력을 관리할 수 있는 모듈이다.

반응형

'Framework > Node.js' 카테고리의 다른 글

[Node.js] ejs  (0) 2021.05.01
[Node.js] Mongoose  (0) 2021.04.26
[Node.js] MongoDB CRUD  (0) 2021.04.25
[Node.js] 파일 업로드  (0) 2021.04.24
[Node.js] 요청 라우팅 - Router  (0) 2021.04.23
Comments