Running Node.js

Aditi Dosi
4 min readNov 28, 2022

--

For running Node.js:

Command -

node (Run the Node REPL in your terminal)

node — version (Print your current Node version)

node filename.js (Execute the Node code in filename.js)

💡 REPL stands for Read Eval Print Loop. This is the list of steps that happen when you run the node command and then type some code.

Node.js Global Object

In Node, we have a global object that we can always access. Features that we expect to be available everywhere live in this global object.

For example, to have some code execute after 5 seconds we can use either global.setTimeout or just setTimeout. The global keyword is optional.

setTimeout(() => { 
console.log('hello');
}, 5000);

Probably the most famous global is global.console.log which we write as just console.log.

Node.js Module System

The require Function, Built-in Modules, Creating Modules, ECMAScript Modules

In Node.js, each file is treated as a separate module. Modules provide us a way of re-using existing code.

The require Function

We can re-use existing code by using the Node built-in require() function. This function imports code from another module.

const fs = require('fs');
fs.readFileSync('hello.txt');
// OR...
const { readFileSync } = require('fs');
readFileSync('hello.txt');

Built-in Modules

Some modules like fs are built in to Node. These modules contain Node-specific features.

Key built-in modules include:

  • fs — read and write files on your file system
  • path — combine paths regardless of which OS you’re using
  • process — information about the currently running process, e.g. process.argv for arguments passed in or process.env for environment variables
  • http — make requests and create HTTP servers
  • https — work with secure HTTP servers using SSL/TLS
  • events — work with the EventEmitter
  • crypto — cryptography tools like encryption and hashing

Node.js Packages

NPM Commands, package.json, node_modules, package-lock.json

Node developers often publicly share packages, that other developers can use to help solve common problems. A package is a collection of Node modules along with a package.json file describing the package.

To work with Node packages we use NPM. NPM includes two things:

  1. The NPM registry with a massive collection of Node packages for us to use.
  2. The NPM tool that you installed when you installed Node.

Most Node applications we create include a package.json file, which means our Node applications are also Node packages.

The package.json file contains:

  1. Name, version, description, license of the current package.
  2. Scripts to automate tasks like starting, testing, and installing the current package.
  3. Lists of dependencies that are required to be installed by the current package.

Node.js Event Emitter

Node.js provides a built-in module to work with events.

const EventEmitter = require('events');
const celebrity = new EventEmitter();
celebrity.on('success', () => {
console.log('Congratulations! You are the best!');
});
celebrity.emit('success'); // logs success message
celebrity.emit('success'); // logs success message again
celebrity.emit('failure'); // logs nothing

Many features of Node are modelled with the EventEmitter class. Some examples include the currently running Node process, a running HTTP server, and web sockets. They all emit events that can then be listened for using a listener function like on().

For example, we can listen for the exit event on the current running process. In this case, the event has a code associated with it to be more specific about how the process is exiting.

Backend Concepts

Client-server architecture

Your frontend is usually the client. Your backend is usually the server.

In a client-server architecture, clients get access to data (or “resources”) from the server. The client can then display and interact with this data.

The client and server communicate with each other using the HTTP protocol.

API

Short for Application Programming Interface.

This is the set of functions or operations that your backend server supports. The frontend interacts with the backend by using only these operations.

On the web, backend APIs are commonly defined by a list of URLs, corresponding HTTP methods, and any queries and parameters.

CRUD

Short for Create Read Update and Delete.

These are the basic operations that every API supports on collections of data. Your API will usually save (or “persist”) these collections of data in a database.

RESTful

RESTful APIs are those that follow certain constraints. These include:

  • Client-server architecture. Clients get access to resources from the server using the HTTP protocol.
  • Stateless communication. Each request contains all the information required by the server to handle that request. Every request is separate from every other request.
  • Cacheable. The stateless communication makes caching easier.

Express.js

GET Routes, POST Routes, Routers

Node.js Folder Structure

One typical folder structure for an API following RESTful architecture and using the Express framework can be found below. Node servers typically follow the Model View Controller pattern. Models live together in one folder. Controllers are grouped together based on which feature or collection they are related to. Views are typically managed by the front end, although some Node servers may serve static HTML or use templating engines like Handlebars.

--

--