How to get Input from stdin / input.txt in Node.js / JavaScript↵
==================↵
↵
### Write this header code in the beginning of your js code↵
↵
```↵
'use strict';↵
↵
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.trim().split('\n').map(string => {↵
return string.trim();↵
});↵
↵
main(); ↵
});↵
↵
function readLine() {↵
return inputString[currentLine++];↵
}↵
↵
// thats all what you have to write to get input from stdin, using readLine.↵
↵
↵
// Main code runs in main();↵
↵
↵
function main() {↵
const parameterVariable = readLine();↵
const line2 = readLine();↵
↵
greeting(parameterVariable);↵
greeting(line2);↵
}↵
↵
function greeting(parameterVariable) {↵
console.log('Hello, World!');↵
console.log(parameterVariable);↵
// Write a line of code that prints parameterVariable to stdout using console.log: ↵
}↵
↵
```↵
↵
### Testing Locally↵
↵
paste your input in a text file input.txt↵
↵
then↵
```↵
$ cat input.txt | node main.js↵
``` ↵
to view on console.↵
```↵
$ cat input.txt | node main.js > output.txt↵
```↵
to store the output in file.↵
↵
##### you can also make a sublime-build file for this and then just press Ctrl+B ↵
==================↵
↵
### Write this header code in the beginning of your js code↵
↵
```↵
'use strict';↵
↵
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.trim().split('\n').map(string => {↵
return string.trim();↵
});↵
↵
main(); ↵
});↵
↵
function readLine() {↵
return inputString[currentLine++];↵
}↵
↵
// thats all what you have to write to get input from stdin, using readLine.↵
↵
↵
// Main code runs in main();↵
↵
↵
function main() {↵
const parameterVariable = readLine();↵
const line2 = readLine();↵
↵
greeting(parameterVariable);↵
greeting(line2);↵
}↵
↵
function greeting(parameterVariable) {↵
console.log('Hello, World!');↵
console.log(parameterVariable);↵
// Write a line of code that prints parameterVariable to stdout using console.log: ↵
}↵
↵
```↵
↵
### Testing Locally↵
↵
paste your input in a text file input.txt↵
↵
then↵
```↵
$ cat input.txt | node main.js↵
``` ↵
to view on console.↵
```↵
$ cat input.txt | node main.js > output.txt↵
```↵
to store the output in file.↵
↵
##### you can also make a sublime-build file for this and then just press Ctrl+B ↵