DEV Community

Cover image for Nodejs Express Project Structure For REST API
Srujal Patel
Srujal Patel

Posted on • Updated on

Nodejs Express Project Structure For REST API

Hello all,

Before I start about the NodeJs project structure let me introduce myself. I am Srujal Patel, the Full Stack Web Developer.

Do find for Nodejs project structure and you will find very few structures.
In fact, most of the Nodejs tutorials also won't be using any proper file structure.

Today I will explain to you my own Nodejs project structure created using express REST API.

Let's start it will take 5 to 10 min read the whole blog and implementation will give you a real idea about this project structure.

File Structure Screenshot

The first config folder contains all the configuration variables JSON files.
Config Folder Demo Screenshot

Constant folder contains the const variable which can be used in the whole project. I have used two demo versions - one for sending the static message in response and the parent route name.
Constat Folder Sample

Cors folder work as middleware in the whole project. I have created some sample middleware in the project like sending emails, check token for each and every route, and send the response after REST API execution has been finished.
Cors Folder Sample

The routes folder is having 2 files one for creating the routes and another file is known as the controller.
The controller file uses for controlling the flow like calling the service, taking the request and sending the response as well.
Routes Folder Sample

The Service folder is having files for creating services.
This file can be divided into two parts one for database related query and second for after getting the data from the database do some coding for business logic. But here I have considered both tasks will be kept in one place. (it's up to you if you want to divide, go for it).
Service Folder Sample

The upload folder is having all the static file which can be used to retrieve without any API calling or basically without any authentication.

Then the validation folder is having all the routes validation.
Validation File Sample

There is a server file which is I think the heart of the node-express application. Server file will have the DB connection, starting the node-express application and many more.

I Hope this file structure will help you to build projects.

If you like this structure please don't forget to give me a star on Github.
If you have any issue, suggestion, idea or modification please use GitHub to create an issue or pull request.

GitHub logo iamsrujal / nodejs-file-stucture-express

Nodejs Express Project File Structure For REST API

Nodejs File Stucture Express

Nodejs Project Structure For Express REST API

General Info

Sample Nodejs Project file structure architecture created by Srujal Patel

Testing Locally

Make sure you have installed Nodejs, npm and git.
Note: npm is installed with Node.js

git clone https://github.com/iamsrujal/nodejs-file-stucture-express.git

cd nodejs-file-stucture-express

npm install or npm i

npm run dev (For Development environment) 

OR

npm run staging (For Staging environment)

OR

npm run prod (For Production environment)
Enter fullscreen mode Exit fullscreen mode

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

You can check out the full license here

MIT




Top comments (0)