Giter VIP home page Giter VIP logo

archetype-be-js's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.