Created
January 6, 2018 09:55
-
-
Save SuperHuangXu/aed3e456800ff17defdd44e1532bbfdf to your computer and use it in GitHub Desktop.
nodejs输入输出
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as readline from 'readline'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
function scan(): Promise<void> { | |
return new Promise((resolve, reject) => { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
// 提示,默认为 '>' | |
rl.prompt(); | |
rl.on('line', (line) => { | |
resolve(line); | |
setTimeout(() => { | |
rl.close(); | |
}, 500); | |
}); | |
rl.on('close', () => { | |
process.exit(0); | |
}); | |
}); | |
} | |
scan().then(data => { | |
fs.appendFile(path.join(__dirname, 'hello.txt'), data, { | |
encoding: 'utf8', | |
}, (err) => { | |
if (err) { | |
throw err; | |
} | |
return this; | |
}); | |
}).then(() => { | |
fs.readFile(path.join(__dirname, 'hello.txt'), (err, data) => { | |
if (err) { | |
throw err; | |
} else { | |
console.log(data.toString()); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment