Steps to create files & folders
1. Root folder
Make a root folder. It is generally your project name. For eg. I want to create a YouTube clone so my root folder name is YouTube Clone.
2. Initialization
Because we are using MERN stack technology to create the backend. So just open the terminal and type npm init. Here npm stands for node package manager & init for initialization. The complete statement says that we are initiating the node package in the root folder. As you enter the command it asks some questions like project name, author name, keywords, git repository, etc. After that, it creates a package.json file in the root folder with some pre-default code.
3. README.md
Now you should create a README.md file. It is because when you create a project in a team, all the members can read this file and better understand the project & its working.
4. Gitkeep File
As a backend developer, it usually occurs that you have to upload an image, video & file in your service database like AWS or Cloudinary. But in some cases, when the user uploads files or images due to some network issue or unresponsive browser has to re-upload the whole stuff again. So at the production level, to avoid this scenario, firstly files are uploaded on the server and after that, it uploaded to the database (AWS, cloudinary). So here we need to create a public folder & within this folder, we need to create a temp folder also. Within the temp folder, you must create a .gitkeep file. It is because when you push the codebase to github, Git Hub can’t track empty folders. So without the .gitkeep file, the public folder remains untracked and can’t be uploaded on Git Hub.
5. Env File
As a backend developer, you have to tackle a lot of APIs, secret keys, access tokens, expiry tokens, and user credentials (password, UPI). However, these credentials must not be displayed to the public & not be uploaded on Git Hub. So to hide these you need to create a .env file where env stands for environmental variable.
6. Gitignore File
As mentioned above, the .evn file must not uploaded on Git Hub. There is also another file that should not uploaded on GitHub like node modules. So to remain untracked these files, you have to create a .gitignore file & list out all the files & folders that you want to remain untracked.
7. Src Folder
All the above files are created in the root folders. But now the structure of making actual files & folders starts. These are the files that contain actual programming & business logic. In production, it is recommended that these files & folders be created within a folder named src ( source) for segregation of files.
Now create three files name server.js, index.js & constant.js
- server.js:- code written to run the server.
- index.js:- code written to connect servers, databases & API’s.
- constant.js :- code written to create & manage some constant variables.
- Besides files, we also need to create some folders named model, utils, middleware, db, route, and controller.
- DB:- code was written to make the database connection, whether the database is Mongo dB or MySQL.
- model:- code written to create a model & define structure to store data.
- middleware:- code written here which works between API request & controller. For eg. user verification.
- route:- all the route codes written here like “/login”, “/post”, “/sign”.
- controller :- Actual core programming & business logic code written here.
- util:- code written which is used as utilities like email, uploading files.
There may be slight differences in names & file count.
Production Level Directory Setup for Backend
When we make a project, even a small project there are a lot of files & folders that have their importance. But to maintain it is very hectic. So It is an approach that is used by tech giant & startups to make their codebase easy, maintainable & structured.
If you use this approach it will drastically reduce the files’ organization complexity & also be appreciated. So in this article, we’ll explore production-level setup in detail, its advantages, and many more.