[node. js] Pass command line arguments to node. js

Source: Internet
Author: User
Tags node server

Command line arguments is often used to modify the behavior of a application or specify needed parameters for operation. In this lesson, you'll learn how to access the command line arguments passed to your node. JS application as well as dif Ferent strategies for evaluating and accessing them.

To catch what arguement user passed on from command line, we can use 'process.argv '.

If We log out:

// Server.js Console.log (PROCESS.ARGV)

Then we run the command:

Node server.js foo Bar soo

In the console, it logs out this:

' /usr/local/bin/node ' ,   ' /users/zhentianwan/documents/programming/node/nol/server ' ,   ' Foo ' ,   ' Bar ' ,   ' Soo ' ]

Here the ' foo Bar Soo ' is meaningless, but in the real case, the argv user pass in might is something we need, but the OR Der might is differet, for example, sometime-do:

NPM I ramda--save

Somtime you might:

NPM I--save Ramda

The Order of command can be different.

To make sure what user have pass in, we can do:

if (Process.argv.indexOf ('foo')! =-1) {Console.log ('yeah! We is going to foo! '); }

[node. js] Pass command line arguments to node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.