Node.js 命令行传参
nodejs中也可以像别的语言比如C++那样传参,我们需要process.argv.forEach((val, index) 来获取参数,并处理。
process.argv.forEach((val, index) => {
//do some handles
})
举个例子:
process.argv.forEach((val, index) => {
console.log(`${index}: ${val}`)
})
Node.js输出
使用console.log输出:
age=17
console.log("I am orbitgw");
console.log("I am %d years old",age);
顺便提一嘴,nodejs大多交互都是在网页里进行的,输出有时只是为了调试。所以输入并不常用而且非常麻烦,要引入外部模块readline
:
const readline = require("readline");
let r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
})
r1.question("What's your name? ", function (answer) {
console.log("Name: ", answer);
r1.close();
})
r1.on("close", function () {
process.exit(0);
})