How can I pass arguments into node.js javascript file? -
i totally new node , have been reading process api site. not think have context down yet on getting better understanding.
i want pass argument node script, command line node connecttoerver.js -ip 192.10.10.1. have code below kind of works reading docs not know going on here.
process.argv.foreach(function(val, index, array) { console.log(index + ': ' + val); });
your example node docs, process.argv
(argv stands arguments vector) is
an array containing command line arguments. first element 'node', second element name of javascript file. next elements additional command line arguments.
so output connecttoerver.js -ip 192.10.10.1
going be:
0: node 1: path/to/connecttoerver.js 2: --=ip 3: 192.10.10.1
process.argv
array of command line arguments passed script. first 1 node
, second filepath , others arguments passed script separated white space.
what want library parse arguments , pass in handy way. example optimist you'll get:
var argv = require('optimist').argv; if (argv.rif - 5 * argv.xup > 7.138) { console.log('test one'); } else { console.log('test two'); }
call like
node xup.js --rif=55 --xup=9.52 test 1 node xup.js --rif 12 --xup 8.1 test 2
such libraries include argument parsing, default values, making var required, etc.
Comments
Post a Comment