Node.js Backend web service/API. Part 1

Okafor Emmanuel
3 min readApr 26, 2019

--

So over the years, I have been able to adopt an interesting approach to developing a backend API for clients. Well, I was lucky most of the project I worked with have been from scratch, meaning we had to discuss a whole lot on the architecture and design approach before development. I feel obligated to share a few decision that has worked for us and I find useful and worthwhile. And I hope to get comments and more insight from you guys to further help me learn and become better. Yes, we are all learning.

What we want to do.

A todo backend web service/API will be developed from grounds up leveraging on the concept of OOP for scalability and extendability without sacrificing comprehensive architectural design and code quality.

Who will benefit from this tutorial

This article is for intermediate to advance backend developers who understand the basic approach to building a backend restful API. You can skip this article if you can go through the source file here and understand everything you need. I know you can,

This article will be broken into 4parts.

  1. Part 1. Overview of dependencies, project setup and server setups.
  2. Part 2. Architectural decision and design concept.
  3. Part 3. Utility classes and function and the concept behind them
  4. Part 4. Implementation of the controller function

Main Dependencies

  1. Nodejs must be installed
  2. Mongo DB database
  3. Babel

Overview of dependencies, project setup and server setups.

The src folder contains the source file of the project. the purpose of each of the subfolders will be explained subsequently.

The package.json file and the basic dependencies

A config folder to help specify variables required application check here to have a better understanding of how the environment variables are loaded. here is the default config file.

So before we get on it, here is a snapshot of what the entry file will look like. Yes, I just wanted to show this because I really don’t have a clue on where to start because I will be making a lot of assumptions that the reader is already familiar with most of the concept and have prior knowledge as stated above.

app.js

From the above code, the entry point starts by initializing the database, then the express followed by the defined routes before the server was finally created. the files required to set up the server is in the setups folder

database.js

express.js

server.js

The API routes are loaded from the index.js file within the API folder. this file loads all the routes with the API prefix specified in the app config file. In the file, the API authentication middleware was loaded first to ensure client authorization.

ApiAuth middleware

I think we should have an overview of how the server was set up and the requirements before we dive into the intricacies of the project in the next part.

Part 2 will discuss the architectural decision and design and show the concept adopted. Please comment below and if you have an idea on how I can make this article better. Thanks and see you in the next part

--

--