Node JS - Project Structure | Boilerplate | Express Generator Utility

Usually developers work with the ready made project’s code provided in company which was created by someone else or developed long time back. 
But When it comes to writing their own project from scratch, developer’s mind is looking for following questions. 

  1. What should I begin with? 
  2. How can I make it work well? 
  3. Why should I do this?
In this article, I'll share some main tips about how to set a boilerplate for an API based on Express & how to use project structure which can be scalable over a period of time without any issues.


REST API or Backend project which give data to various client such as Desktop, Web, Mobile app etc.

You can use Express Generator Utility to start using out of the box boilerplate project structure and expand as per your need.


Set up express-generator in your local machine and create the directory for your new project.

npm install express-generator -g
express <your-project-name>

e.g. Open command prompt or Terminal tab in Visual studio code IDE.
Run below command

$ express PROJECT_NEW_API

PROJECT_NEW_API
├── app.js
├── bin
│   └── www
├── package.json
├── src
│   ├── routes
│   ├── controllers
│   └── models
│   └── middleware
│   └── utils
├── public // Copy Content of Angular project here after build (/dist)
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js

The above generated boilerplate project structure. You can made changes as per your need.
Let’s go through the structure of the project in one by one.

I've made few changes as per my requirement in generated code.

Controllers  — In this folder you can store all your Rest API endpoints

Models  —  In this folder you can store all the data models (Interface structure)

Utils  — Here you can put all the helper and supporting code of the app is stored (Constants, Mailer class, File download, PDFs generation, etc.)

Middleware —  Here you can find all Express middle ware of the application like validation of JWT, OAUTH etc.

MONGO or MYSQL  or MSSQL or <yourDatabaseName> — Here it contains all the work with your database connection string , pooling etc.

Сonfig or .env — This will be used to store all your app’s web config settings in a separate file (such as  .js  or .Json or .env). Its as per your need


So here in this article I've mentioned How to create a boilerplate Project strucrure using Express-based application. 

Always keep in note that these are not the hard rules to follow, but just to make sure that you can refine the structure of your API.

Thanks for reading the article.

Happy Learning!

Comment if you have any doubt.

Comments

Popular posts from this blog

PUTTY - The server's host key is not cached in the registry cache

OIM-12c Installation - FMW - SOA - IDM

SAML & OAuth 2.0