Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Events

    5 Best JavaScript Conferences to Attend in 2019

    JavaScript

    A Complete reference guide to Redux: State of the art state management

    Startups

    Maximizing Startup Success: Strategic Data Utilization Techniques

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / 24. Node.js Lessons.Reading Parameters From the Command Line and Environment.
    Programming

    24. Node.js Lessons.Reading Parameters From the Command Line and Environment.

    bragin_paBy bragin_paNovember 21, 2016Updated:May 26, 2024No Comments4 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    24. Node.js Lessons.Reading Parameters From the Command Line and Environment.
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link

    24_s1

    Hey all! The first topic of this article is transferring of parameters and the script for Node.js. To show you the principle, we will create a file with a simple code, so add it (server.js):

    // node server.js port=3000
    
    var http = require('http');
    
    http.createServer(function(req, res) {
    
        res.end("The server is running!");
        
    }).listen(3000);

    and will order it to take a port for launching from a command line to launch it in the following way:

    node server.js port=3000

    and this port will be used for listen.

    To do so, we can add this parameter to the launch configuration in your IDE (add port=3000). In order to get what has been transferred in a command line, you can perfectly use a variable called process.argv.

    // node server.js port=3000
    
    var http = require('http');
    
    console.log(process.argv);
    
    http.createServer(function(req, res) {
    
        res.end("The server is running!");
    
    }).listen(3000);

    Launch it. Now you see the outputted parameters. Note that the first place belongs to node itself followed by the file and further by the port. Here you can easily see that the simplest ways to get the parameter value is to look through an array and get out the respective string. There is a more convenient way – to install a module called optimist:

    npm install optimist

    Use optimist instead of process.argv, and we will see the results. To do so, I need to change the syntax because optimist supports either this kind of syntax:

    // node server.js —port=3000

    or:

    // node server.js –port 3000

    // node server.js port=3000
    
    var http = require('http');
    
    console.log(require('optimist').argv);
    
    http.createServer(function(req, res) {
    
        res.end("The server is running!");
    
    }).listen(3000);

    Now I get the options through argv. Launch. As we can see, everything’s perfect. The only thing left to do is to use a variable and change the port within listen:

    // node server.js port=3000
    
    var http = require('http');
    
    var opts = require('optimist').argv;
    
    http.createServer(function(req, res) {
    
        res.end("The server is running!");
    
    }).listen(opts.port);

    Restart and see how our server is working with the new port.

    Read More:  5 Website Security Threats and How to Counter Them

    http://localhost:3000/

    It’s perfect!

    So, our next step now. Here node is used for launching, but we usually use another module for development – for example, supervisor, to enable it to restart the server automatically. Respectively, to use supervisor and transfer parameters, you need to do launch it as follows:

    supervisor -- server.js --port=3000

    Note: double hyphen is obligatory. The system won’t work without it. So, let us do respective changes within configurations. The supervisor module must be global. Launch it. Supervisor has reported to us what it’s been doing.

    So, we can get the parameters of the command line, first of all, from the array process.argv; second, from module optimist, and third, you can get the settings from the environment variables. In general, most of them are set by the operating system – in particular, the HOME-named environment variable can be found almost anywhere. We can get it as follows:

    // supervisor -- server.js --port=3000
    
    console.log(process.env.HOME);
    
    var http = require('http');
    
    var opts = require('optimist').argv;
    
    http.createServer(function(req, res) {
    
        res.end("The server is running!");
    
    }).listen(opts.port);

    So, launch it. And here it outputted a home directory of the current user.

    You can use your own environment variables. For example, within the Express framework that we will study a little bit later, a  environment variable named  NODE_ENV–  is used. It stores information on what mode the launch has got. For example, NODE_ENV=development is a development mode or production is a mode of a live server. In the code, we can check: whether it is NODE_ENV = production, we can apply extra optimization, otherwise, if it is NODE_ENV = development, we can connect additional debugging output.

    // supervisor -- server.js --port=3000
    
    console.log(process.env.HOME);
    
    var http = require('http');
    
    var opts = require('optimist').argv;
    
    http.createServer(function(req, res) {
    
        if (process.env.NODE_ENV == 'production') {
            // optimization
        } else if (process.env.NODE_ENV == 'development') {
                // additional debugging output
    
            }
        
        res.end("The server is running!");
    
    }).listen(opts.port);

    In order to set the  environment variables we will use one of the several ways. The first one is: if the launch is via IDE, you can set up the environment variables  right there. Whether the launch is outside IDE, indication of  environment variables is executed differently, depending of the operating system. For example, it will be the following for Windows:

    Read More:  How I Built an Admin Dashboard with Python Flask

    set NODE_ENV=production

    All further commands launched through the terminal will have the value NODE_ENV=production. Under Unix systems with Bash wrapping the analogue will be the following:

    export NODE_ENV=production

    Every further launch will inherit the variable. If you want to execute only one launch with this exact value, your  environment variable is equal to the value and a line further:

    NODE_ENV=production supervisor -- server.js --port=3000

    So, we’ve analyzed three ways to get process parameters in Node.js:

    1. Out of the command line through process.argv.
    2. Out of the command line, but via optimist module that can handle some standard setting types.
    3. Getting parameters out of  environment variables contained in process.env.

    The lesson code can be downloaded from here.

    node-24_2

    The materials for this article were borrowed from the following screencast

    We are looking forward to meeting you on our website blog.soshace.com

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    bragin_pa

      Related Posts

      Mastering REST APIs: Essential Techniques for Programmers

      December 18, 2024

      Crafting Interactive User Interfaces Using JavaScript Techniques

      December 17, 2024

      Effective Strategies for Utilizing Frameworks in Web Development

      December 16, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      JavaScript August 31, 2020

      How to Creact a React Component Library – Using a Modal Example

      React component libraries exports various reusable components for our react projects. In this article, we’ll learn how to create our own library using a Modal example.

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Analyzing Leadership Styles and Their Influence on Project Success

      November 25, 2024

      Transforming LinkedIn Connections into Viable Sales Leads

      November 25, 2024

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      What is Mentorship in Web Development | How to Find a Coding Mentor

      Tips July 24, 2019

      The Full Guide to the New Excel XLOOKUP Function

      Beginners February 1, 2020

      Startup Spotlight: Five Companies That Revolutionize Healthcare & Wellness

      Startups March 12, 2019

      Leveraging Video Recruiting to Attract Top Talent Effectively

      Recruitment November 24, 2024

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      LinkedIn

      Effective Strategies for Following Up with LinkedIn Prospects

      JavaScript

      Responsible Web Scraping: Gathering Data Ethically and Legally

      LinkedIn

      Strategic Methods for Building a LinkedIn Prospect List

      Most Popular

      Nurturing LinkedIn Prospects Through Consistent Engagement

      LinkedIn

      The Ultimate Introduction to Kafka with JavaScript

      JavaScript

      Visualizing Logs from a Dockerized Node Application Using the Elastic Stack

      JavaScript
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.