Skip to content

Archetype Javascript Backend, NodeJS Express MongoDB Modular Structure

License

Notifications You must be signed in to change notification settings

ethyaan/archetype-be-js

Repository files navigation

Archetype NEMBBMS

NodeJS ExpressJS MongoDB Boilerplate Backend Modular Structure - pure JS

made-with-javascript MIT license Maintenance Generic badge

NodejS Backend structure modular JS

Node JS Backend Application with Modular code structured

This is a ready and production level tested structure worked with great perfomance and efficency.

Table of content:

  1. The Idea
  2. How it works
  3. Requirement
  4. How to run
  5. modules
  6. services
  7. Author
  8. License

Our main objective is to establish a strong structure for the backend application using Express.js. Our top priorities include a modular and decoupled code structure, a scalable approach for REST API development, and following best practices for writing clean code and implementation.

Additionally, as many common business logics and functionalities are repetitive, we have made the decision to implement each of these scenarios only once to save time and effort in the long run.

Here we have a Node.js Backend application that produces REST API endpoints.
It's entry point is index.js file, the main modules that we uses are Express ' moongoose. we have our express app related codes and configurations inside app.js file and it exposes the app instance to index.js.
When you run the apllication first it tries to connect to MongoDB server.
And then finally it start the the app on configured port which is configured inside .env configuration file.

The application code structure is as below:

app
  - common
  - modules
    -- module1
    -- module2
    -- ...
  - services
  - emails
    -- html
    -- mjml

Common : here is the place that we keep out commonly used codes and tools like, FactorClasses. Global Validators, Loggers and ...

Modules : contains modules folders, each folder inside this directory is treated as one module and it should has neccessary file structure that is required to load properly in the express app.
remember it's better to have each modules specified for one task.
Each module that we create is going to loadd automatically. no need to import anything anywher.

Servies : contains the service files, Each service file is a class that has member specefied to do a specefic functionality, example google-captcha, authentication, sendemail, logger, payment, sendgrid or ...

emails : To use MJML, you can create a new MJML file with the .mjml extension, and then compile it to HTML using the script in package.json file

Each module is a folder and it usually has the following structure.

  user
    - controller.js
    - route.js
    - schema.js
    - [*].yaml
    - validator.js

controller.js is where the business logic resides, all of our logical operations will be handled inside controller.
which existance of route.js is neccessary for each module, because autoloader will look inside each module folder to load route.js which is the entry point for each module.
route.js is a file that contains all the routes for that modules, exposes a function that create a new instance of the route class file.
schema.js is the file that contains our mongoose Schema files for MongoDB.
*.yaml any file with .yaml format inside module directory is an OpenAPI speceficiation for the rest APIs which will be used by Swagger-UI.
validator.js all of the API validation for the module are defined here by using express-validator

sample-module folder is a example module which you can use as template, it's tiny and doesn't contain any logic.

Node.JS -> v16.15.1 MongoDB -> tested with > 4.2.8

First get a copy or clone or download the project.
In the root folder, copy .env.sample to .env file and modify the configuration based on your desire.


Configuration Options

PORT : the application port

MONGO_URI : MongoDB connection string
the dault value is set to point to the local docker MongoDB container

Run Local Docker MongoDB Containe

first make sure have the docker engine installed and runing on your machine
npm run db:up : brings up the MongoDB container
npm run db:down : stops the Mongodb container
npm run db:purge : removes the container

DBNAME : set your MongoDB databse name here

VERIFICATION_CODE_LIFE_TIME : time to expire of the user verification code in minutes since it get generated, used in user module

JWT_SECRET : JSON Web Token Secret signing key, makesure to change this

SENDGRID_API_KEY : This environment variable is used to authenticate your application with the SendGrid email service API.


Install the dependecies by npm i or yarn command

Then run npm run start Or yarn start to start the application
API documentation is available by Swagger under http://[host]:[port]/docs

... updating...

... updating...


Ehsan Aghaei

MIT License.

About

Archetype Javascript Backend, NodeJS Express MongoDB Modular Structure

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •