📝 기본 코드
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", (line) => {
console.log(line);
rl.close();
});
rl.on('close', () => {
})
📕 readline 모듈
✔️ readline 모듈 불러오기
const readline = require("readline");
✔️ 인터페이스 객체 생성
const rl = readline.createInterface();
✔️ 표준 입력과 표준 출력을 지정하는 부분
input: process.stdin,
output: process.stdout
📕 rl 변수
✔️ 입력
Enter를 누르면 한 줄씩 입력받고 line에 저장
rl.on("line", (line) => {
✔️ 조건
특정 조건에서 입력값을 여러번 받고 싶을 때
rl.close()가 없으면 값을 무한으로 받음
if () {
...
} else {
rl.close();
}
✔️ 출력
입력이 끝난 후 실행할 코드
process.exit()은 선택 사항
rl.on("close", () => {
process.exit();
});