Accepting Input
Video Course on Real-Time HTML5 with Node.js
accept input from the command line
var name = process.argv[2];
// argv[0] is the node install path
// argv[1] is the running file path and name
// then
console.log("hello", name);
then run node 1.js World
create a helper module to handle parameters parsinguses
var args = require("minimist")
it automatically parses parameters
var args = require("minimist")(process.argv.slice(2), {string:"name"})
we immediately pass the command line arguments (removing the first two)
so we can have
var name = args.name;
then we can run:
node 1.js --name=World
we can even replace bash scripts with this
#!/usr/bin/env node
now you can
chmod 700 1.js
and just run $ ./1.js --name=World
in the terminal, don't need to add node to the command