Categories
Node.js Basics

Node.js Basics — App Configuration

Spread the love

Node.js is a popular runtime platform to create programs that run on it.

It lets us run JavaScript outside the browser.

In this article, we’ll look at how to start using Node.js to create programs.

Configuration

We can configure our Node app in various ways. We can use it to make our app more flexible.

JSON Files

One way to configure our Node.js app is to read the configuration from one or more JSON files.

For example, we can configure our HTTP server from a JSON file by writing:

config.json

{
  "host": "0.0.0.0",
  "port": 8000
}

index.js

const { port, host } = require('./config.json'),
  http = require('http');

http
  .createServer(function(request, response) {
  })
  .listen(port, host, function() {
    console.log('Listening on port', port, 'and host', host);
  });

We read the configuration from the config.json file.

It’ll automatically be parsed into an object when we call require with the path.

Then we can call createServer and listen methods with it.

We can store multiple configurations with it.

For example, we can write:

config.json

{
  "server": {
    "host": "0.0.0.0",
    "port": 8000
  },
  "database": {
    "host": "db1.example.com",
    "port": 27017
  }
}

index.js

const { server: { port, host } } = require('./config.json'),
  http = require('http');

http
  .createServer(function(request, response) {
  })
  .listen(port, host, function() {
    console.log('Listening on port', port, 'and host', host);
  });

We have a server and database property so that we can store the config for the HTTP server and the config.

Then we destructured the property we need for the HTTP server.

Environmental Variables

We can store our config as environment variables. This way, we can store the settings in our server’s OS or in a file and read them from there.

To do that, we can use the dotenv library.

We can install it by running:

npm i dotenv

Then we can use it by creating and .env file:

HOST=0.0.0.0
PORT=8000

index.js

const http = require('http');
require('dotenv').config()
const { PORT, HOST } = process.env;

http
  .createServer(function(request, response) {
  })
  .listen(PORT, HOST, function() {
    console.log('Listening on port', PORT, 'and host', HOST);
  });

We read the environment variables from the process.env property after we call:

require('dotenv').config()

We store the key-value pairs in the .env file and then read them with the code above into the process.env property.

Arguments

A Node app can also take in arguments.

The command-line arguments will be put into an array and assigned to the process.argv property.

It’ll contain the parts of the command split by spaces.

For example, if we run:

node server.js --port=8001

then process.argv is:

['node', 'server.js', '--port=8001']

We should set some default values if there’s no value set for the command-line argument.

For example, we can write:

const http = require('http');
const [,, PORT = 8080, HOST = '0.0.0.0'] = process.argv;
http
  .createServer(function(request, response) {
  })
  .listen(PORT, HOST, function() {
    console.log('Listening on port', PORT, 'and host', HOST);
  });

We assign the default values as we’re destructuring the command line arguments.

Conclusion

We can configure our Node app in various ways.

Methods include using JSON files, environment variables, and command-line arguments.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *