我仍然不熟悉nodeJs,并且尝试创建输入流,但是一旦我通过调用node fileName在终端中启动应用程序,我的代码就无法正常工作。
我的输入格式如下:
- N the number of queries.
- second line represent a string containing two words separated by a space.
- N lines of a string containing two words separated by a space.
由于某种原因,终端没有显示任何东西。
这是我的代码:
'use strict';
const fs = require('fs');
var n = 0;
var position = '';
var destination = '';
var array = [];
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = parseInt(readLine(), 10);
const input= readLine().split(' ');
position = input[0] ;
destination = input[1];
console.log('our number is',s, 'and our position and destination:', position, destination);
ws.end();
}
答案 0 :(得分:1)
您可以通过允许readline缓冲输入来简化整个过程:
'use strict';
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', line => {
rl.write(
line.replace(/\s*$/, '')
);
});