Express.js and its File Structuring : Folder Scaffolding
Tags: node, mongodb, javascript, architecture
Published: 2020-06-28 15:04:54

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5vzr2cps82xkoamlm9qy.png)

================================

The app/file structuring is one way to be Clean Coder. There are no standard structures as such, but, generally followed structure will be discussed below.

I am a beginner. As a beginner, I would explain how did I go about this File Structuring scenario for the Express Application. I have used Express Framework, with MongoDB database. I had used Mongoose as MongoDB Object Modelling for Express Application.

“ There is a luxury in self-reproach. When we blame ourselves we feel no one else has a right to blame us. ”  
\- Oscar Wilde, The Picture of Dorian Gray

It is best to use an Application scaffolder to get you generalised initial structure. I would suggest, go with Express Application Generator or Yeoman Application Generator.

The generated app would have directory structure something of this sort:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b19r1f9puza4petmlv6q.png)

Here, you can create another folder named “src”.

Copy and paste the routes directory to the src folder.

And also create folders namely:

1.  **_Models_**
2.  **_Routes_** // that already exists
3.  **_Controllers_**
4.  **_Services_**
5.  **_Repositories_**

These are the basic files that ‘ src ’ folder/directory will be holding.

Now, you would wonder what would each of directory hold and do?

*   **_Models directory_** will be holding files that keeps the schema/data models that is required for your current project.
*   **_Routes directory_** will be holding the route files, where _Routing_ refers to how an application’s endpoints (URIs) respond to client requests. It basically defines your app routes.
*   **_Controllers directory_** will be holding the controller files, wherein the controller files deal with validation of the data, whatever that is been sent from the frontend. The request and response handling is taken care in this directory.
*   **_Services directory_** will be holding the services files, which deals with the business logic of the API. This is a final filtration before sending it to the Data Access Layer, here we filter the data received into the final payload for querying with the database. The files in this directory will deal with processing the payload to the format, it should be in to be stored into the database.
*   **_Data Access Layer/Repositories_** will be dealing with queries that have to be executed based on API. All the CRUD operations for APIs are taken care of in this particular folder.

These are basic application structuring method I opted to go with.

This method of writing code is a stepping stone to writing clean code. Clean code doesn’t have to deal with solving complex logic in a simple way or writing code neatly. Clean coding is an art of writing the code in such a way that, a beginner himself or person from non-programming background should also be able to understand the flow of the code you have written.

Apart from this, we can also go with various extra folders, which will lie above ‘src’ directory.

Say, your front end is asking for data to be sent from the server in some form. Then, you can build a **_transformation layer/transformation folder_**, where you can have files defining functions converting the data received from the database in one format can be transformed to the format your front-end developers ask for.

Also, you can define a folder named **_helpers,_** which will have some functionality that you want to use in multiple APIs or scenarios. So instead of trying to define it everywhere, you can just define it in one place and call that wherever required.

Another folder could be **_middlewares,_** where you can define all your route-level middle-wares once and call it in your routes as chained-functions.

Make sure you go through some simple concepts like :

*   **_KISS(KEEP IT SIMPLE STUPID)_**
*   **_DRY (Donot Repeat Yourself)_**
*   **_TDA (Tell Don’t Ask)_**
*   **_SoC (Separation of Concerns)_**
*   **_YAGNI (You ain’t gonna need it)_**

I am not 100% sure that this is the standard format. As a beginner, I believe this is the best way to learn to code in Express or any other framework. As a beginner, I had difficulties to know this, as I believed all the logic are to be dumped into controllers. And then, I learnt it later on, that was wrong.

Every problem statement will have its own suitable way of structuring the files. This is a very common method.

I hope the article helps you understand a little about how to structure your back-end application in Express.js.

To get an clear picture how to folder/code would look like, refer to my next article on "How to write CRUD - API in Express", coming soon.

If any queries, please drop a mail mrshravankumarb@gmail.com

Drop your views as comments.

Signing off till next time :)