How to process standard input NodeJS, in the console?

Edu*_*eff 2 javascript node.js

I want to write NodeJS code for solving problems like ICPC. The following is an example using a template of www.hackerrank.com for submitting in JavaScript:

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
    input_stdin += data;
});
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});
function readLine() {
    return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
    var s = readLine();
    s = s.split(",");
    s = s.join(" ");
    process.stdout.write(s);
}
Run Code Online (Sandbox Code Playgroud)

I want to code offline, so I need to run the programs in my Windows console. For runnig the srcript using C:users\user>node path\file.js I added at the end of the code the line

main();
Run Code Online (Sandbox Code Playgroud)

并且脚本运行,但它不处理标准输入。它在“s=s.split()”中给我和错误,错误如下“TypeError: Cannot read property 'split' of undefined”。如果有人知道如何进行节点处理标准输入,请帮助我。

kga*_*har 5

您可以等待SIGINT,SIGINT每当输入流接收到CTRL+C输入时就会发出该事件,通常称为SIGINT。如果SIGINT在输入流接收到 时没有注册事件侦听器SIGINT,则将发出 pause 事件。

SIGINT收到,你的main()函数将被调用和返回后,process.exit()将关闭进程。

所以而不是等待结束事件

process.stdin.on('end', function(){
    input_stdin_array = input_stdin.split("\n");
    main();
});
Run Code Online (Sandbox Code Playgroud)

您可以等待“SIGINT”,如下所示:

process.on('SIGINT', function(){
    input_stdin_array = input_stdin.split("\n");
    main();
    process.exit();
});
Run Code Online (Sandbox Code Playgroud)