Node — Express Js Project Structure Best Practice | Starter Kit
You can always start an express js new project with express-generator
command but even then it requires some additional setup for better structuring your REST-APIs.
In this article, we will discuss one of the ways to structure an express js project.
Here is a glimpse of directory structure of simple authentication project that can be used . This project has some basic setups like user login with email-id and authentication handled by JWT. You will find some files related to these funtionalities.
Folders —
api —This contains routes of all the APIs, here every funtionality has a separate file having collection of different routes related to that functionality, calling business logic functions that are present in services folder. Suppose, we have an e-commerce website then we will create files like product.js, order.js, admin.js, user.js, etc..
Example of user.js handling login, register and profile detail flow —
config — Here we keep our configurations of different libraries/ funtionalities in different files. It contain files like db config, mail server config, payments gateway config etc..
Example of db config file for mongoose —
middleware — This folder contains all the middleware like authentication, error handling, or some other functions you want to add as a middleware.
models — This folder has all the database schema files, individual files for every new collection/ tables.
node_modules — This is node js libraries folder, all the files related to libraries installed are present here.
public — Here we keep all our images, CSS or any other files that needs to be available through public url.
schedulers — Cron jobs like expiring membership of users, sending reminders are kept in this folder.
scripts — All long running scripts are kept here.
services — This folder is where main business logic resides. Routes from API folder will call different functions according to API needs. Divide the logic files based on functionality.
subscribers — Many times we have to perform series of actions which is independent of the main task. For ex — Suppose in a new user registration API, we may want to send welcome email, so instead of waiting for email to be sent and then sending response of register api, we can simply use node js events where on new registration we will fire signup event and send the response of register API. Further actions like sending email/ sms will be handle by subscribers that listen to this signup event. In subscribers folder we have all the events subscriber listening to different events.
Example of userSubscriber file —
Optional Folder —
views — This folder contain layouts of different pages in HTML/ ejs files. We don't need this folder if we are building Rest APIs.
Files —
.env — File containing environment variable values for production server
.env.development.local — File containing environment variable values for local server
app.js — This file is app entry point
.gitignore — Have list of files/ folder that we don't want to include in version control
Dockerfile — Docker settings file
package.json — All dependencies list, scripts settings for npm and project details.
Summary —
Folders — api, config, middlewares, models, schedulers, scripts, services, subscribers
Files — app.js, .env, .env.development.local, .gitignore, Dockerfile
You can use this file structure for your new express js project
Alternatively to get started you can use this starter kit — fork the repo and start your project, repo link — express-starter-kit
That's it, in the next article we will see how to setup a node js server on AWS EC2 instance.