Skip to content

Instantly share code, notes, and snippets.

@SuperHuangXu
Created January 6, 2018 09:55
Show Gist options
  • Save SuperHuangXu/aed3e456800ff17defdd44e1532bbfdf to your computer and use it in GitHub Desktop.
Save SuperHuangXu/aed3e456800ff17defdd44e1532bbfdf to your computer and use it in GitHub Desktop.
nodejs输入输出
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