Bạn có chắc chắn muốn xóa bài viết này không ?
How to Structure Your Nodejs REST API
Developing a well-structured backend API is extremely important for a website. Hence, as an aid for those who are struggling with your backend structure, I will present a backend architecture that I often use down below. This structure is based on the course I learnt from PluralSight - API Design in Node.js Featuring Express & Mongo. I hope some of you might find this blog helpful
I will assume that you already have some experiences with Nodejs before. If you don't, I highly recommend taking a course on YouTube or Pluralsight. I did mention how to learn Nodejs in my previous blog - For newbies - How to get started (Based on my experience).
The picture below is the external structure for your API
As you can see, there is an index.js file. This file will grab the config, app, and logger exported from the server folder and then run the application.
- Create the express app
- Connect to the database
- Clean & Seed the database(import from other files)
- Set up the app middleware (import from other files)
- Set up the routes (import from other files)
- Capture and handle the errors
- Export the express app (index.js needs this)
Each class (model) will be in a different model and the api.js will put these folders together using router.
Router is a very good module in structuring your API. Router is like app in express but with smaller scope. The difference is that you can have multiple routers, each handling a sub path but you can only have one app.
For instance, the router of app.js map the two routers exported from
./session/sessionRoutes with their respective paths (
In each class folder, there are 3 files:
- Model: define the structure of you model
- Controller: define the functions you can use to manipulate the data in this model
- Routes: match the functions defined in controller to the path (using router)
You can have a look at two of my model folders for example (I will attach the link in the conclusion part)
Most of the websites nowadays need athentication/authorization. In my API, I use token authentication and I put it in the Auth folder. You can extend it with Facebook, Google authentication if you want
There are three files in this folder:
- auth.js: Define the functions within the signin process (e.g. verify the user, get the user)
- controller.js: Use the auth.js functions to send the response to the client when they signin
- routes.js: Map the controller function with the path and add middlewares
REMEMBER: You also need to add extra methods to User model in order to encrypt and compare password.
This folder is used to store your pictures, files, etc.
You can host these files using middleware in your middleware folder
This folder consists of things that you will use in many places in you program
For example, I use a separate logger (it looks better ), a seed.js file for cleaning and seeding the database after each time the server restart, and a upload file for uploading pictures.
Although I think this is a really good API structure, you can change it to adapt to your specific project needs if you want.
For the whole backend API, please check it out in my GitHub account. I also developed a REST API in Python - Flask if you like Python
If you have any questions or other ideas about this, please give some comments down below, I really appreciate it. Have fun coding and I will see you in my next blog.
DuyTungHa University of Wollongong, Australia 22-04-2019