Developing Back-End Apps with Node.js and Express

Mahmood Nalim
6 min readOct 24, 2023

What is backend development?

The front end runs on the client machine, and the back end runs on a server.

Backend developers work on the technologies that ensure a website performs correctly, part of which includes writing the code that communicates with the browser’s engine and the backend server.

Furthermore, backend development refers to the development of the server-side logic, including the code that pertains to databases, servers, and applications.

Web servers ensure client requests are responded to, often using hypertext transfer protocol or HTTP for short.

Application servers host and deliver a business application through HTTP. They sit between a database server and a web server. Application servers transform data into dynamic content and run the business logic, which is the data storage and transfer rules.

Backend Responsibilities

Getting started with Node.js

Node.js. is a run-time environment.

The Node.js Javascript code uses the Google V8 engine.

Event-driven, Asynchronous, Non-blocking, Single-Threaded

Processes in a server can be “single-threaded” or “multi-threaded.

Single-threaded is where only one command is processed at a given point in time. Multi-threaded is where multiple commands are processed simultaneously.

Single-threaded, which means it can only do one process at a time. That might make it sound like it is not appropriate for server-side coding but Node.js is asynchronous and non-blocking. This means, while a process is being executed, the program doesn’t have to wait until the process finishes.

Asynchronous -Functions running in parallel with other functions or A process that runs independently of other processes.

Event-driven. When Node.js performs an input/output (I/O) operation, like reading from the network or accessing a database or the file system, an event is triggered. Instead of blocking the thread and wasting the processor's time waiting, Node.js will resume the operations when the response comes back or, in other words, the response event occurs.

Import Vs Require

Node.js applications process

Create a Simple Web Server

Advanced Node.js Modules

There are three types of modules: core, local, and third-party.

Core Node.js modules form a minimal library. They contain the minimal functionality needed to develop Node.js applications.

http -module provides methods to transfer data over HTTP.

fs -module is used to interact with a file system.

OS -module provides methods to retrieve information from the operating system

path -module allows you to retrieve and manipulate directory and file paths.

util -module is intended for internal use for accomplishing such tasks as debugging and deprecating functions.

URL -module is used to divide up a web address into readable parts.

Local modules are the next type of Node.js module. Local modules are the modules written by you and the development team as part of creating your Node.js application.

Third-party modules are available online and have been created by the back-end Node.js community.

NPM-Node Package Manager

Node.js, the difference between development and production

Node.js assumes it’s always running in a development environment.

You can signal Node.js that you are running in production by setting the NODE_ENV=production environment variable.

const env = process.env;
env.NODE_ENV === "production"

Asynchronous I/O with Callback Programming

Callback functions

A function passed into another function as a parameter, which is then invoked inside the outer function to complete an action. Instead of blocking on asynchronous I/O operations, callback functions are used to handle results when the operations complete.

JavaScript is synchronous by default and is single threaded. This means that code cannot create new threads and run in parallel. This is why Callback functions are introduced.

How Callback helps asynchronous …

Issues with Callbacks

Callback Hell -nesting of callback functions forming a pyramid structure. This structure is also sometimes referred to as “The Pyramid of Doom.”

IOC -Inversion of control happens when the flow of control, such as the execution of instructions, is external to your code. Many times, callbacks hand the control over to a third party.

Promises

Axios PackageThe axios package wraps promises around HTTP requests. It returns a promise object.

Working with JSON

JavaScript Object Notation (JSON) data from a hypertext transfer protocol (HTTP) message. JSON is the standard format for application programming interface (API) data exchange.

JSON.stringify() converts a JavaScript object to a JSON string.

JSON.parse() parse a JSON string to a JavaScript object.

Async Await

Promises solved the issues with synchronous programming, nested then can complicate the structure and readability of the code.

In ES 2017, Async/Await was introduced which addressed this issue and gave way to cleaner, readable code.

Web Frameworks

MVC Framework

  • It provides a clear separation of business logic, Ul logic, and input logic.
  • It supports Test Driven Development (TDD).

REST API

REST is a software architectural style that defines the set of rules to be used for creating web services. Web services that follow the REST architectural style are known as RESTful web services.

Express.js

Express.js, or simply Express, is a back-end web application framework for building RESTful APIs with Node.js.

Middleware & Routers

Routing

Middleware

Middleware is also responsible for providing secure connections among services by encrypting and decrypting data, managing application loads by distributing traffic to different servers, and sorting or filtering data before the data is returned to the client.

Five types of middleware are application level, router level, error handling, built-in, and third party.

Authentication and Authorization

The Authentication process confirms a user’s identity using credentials by validating who they claim to be.

Three popular authentication methods in Node.js include:

  1. Session-based
  2. Token-based

Token-based security entails two parts: authentication and authorization.

The user authenticates against the Authorization server. The Authorization server creates an access token and sends the access token back to the client, where the access token is stored. Then when the user makes requests or resources, the token is passed to the resource, also called an API server.

The token contains three parts, the header, the payload, and the signature.

3 . Passwordless

Sample Codes

Find this project : https://github.com/mahmoodnalim/Node-Express-BookReviews-Project

--

--