Premium lesson

Creating the Entry Point

Layered Architecture·4 min read·Jan 1, 2025

An entry point is a point in a program where its execution begins and where it has access to command line arguments.

However, since Node.js doesn't have any built-in equivalent as the main function in the C or Java language, the implementation of such logic is left up to each developer.

Implementing the entry point

Let's start by creating a new file named app.js in the top-level directory, that will contain the application's entry point.

$ touch app.js

A common way to implement an entry point in Node.js is to use an IIFE, which allows us to create an asynchronous entry point through the use of the async keyword, thus enabling the future use of the await operator within the body of the function.

// File: app.js(async () => {  //})();

The next step consists in setting up within this function a try...catch block in charge of catching any eventual error that may occur during the initialization of the application, log that error, and perform a graceful shutdown with a return value of 1:

// File: app.js(async () => {  try {    //
icon light bulb key

Unlock the Build Layered Services in Node.js module

Learn how to build well-structured, easily maintainable, and resilient microservices with the three-layer architecture.

You get immediate access to:

  • 10 focused lessons across the three-layered architecture
  • 1 real-world commented, step-by-step project
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module