• Stars
    star
    288
  • Rank 143,818 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A MERN boilerplate repository using Antd-Pro with RBAC feature, following all production best practices.

Welcome to the RBAC MERN Boilerplate project

A complete (Work in progress now) MERN boilerplate repository with RBAC feature, following all production best practices.

In this repository I will keep adding the production best practices we should follow in a MERN (MongoDB, Express.js, React.js, and Node.js) project. Also for better UX I am using Antd Pro for the UI. May be later I will use MUI as well.

Below are the sample of a sample list page:

Product List Page

Product List Page

Technology stack

As the name suggests, this repository is built on top of Express.js and React.js, however in the implementation detail, we will find other supporting technologies as well.

Client side

  • React - A JavaScript library for building user interfaces
  • Ant Design Pro - A design system for enterprise-level products. Create an efficient and enjoyable work experience
  • Testing Library - React Testing Library
  • Umi Request - HTTP client for the executing HTTP request from browser to server.

Server side

  • Node.js - evented I/O for the backend
  • Express.js - Fast, unopinionated, minimalist web framework for Node.js
  • MongoDB - The application data platform for NoSQL databases
  • Mongoose - mongoose
  • Swagger - Swagger (Not done yet)
  • Jest - JavaScript testing framework
  • Super Test - Super test API testing framework

Details frameworks and packages can be found in the package.json files in server and client directory.

Running the application

This project can be run basically in two ways. One is using docker, other way is to run manually via vscode.

Docker

Depending on the MongoDB hosting option, we choose the appropriate docker-compose file.

Docker compose files

Currently we have two docker-compose files:

  • docker-compose.mongocloud.yml - MongoDB.com hosted cluster
  • docker-compose.yml - Local MongoDB container
Notes
  1. We need to change the MONGODB_CLOUD_URL in docker-compose.mongocloud.yml to the appropriate MongoDB URL.
  2. We need to change the REACT_APP_API_URL in docker-compose.yml to the appropriate API URL. If we want to expose our client to the internet, we need to change the REACT_APP_API_URL to the appropriate API URL. Otherwise keep REACT_APP_API_URL as http://localhost:8002.

Run docker-compose commands

It is expected that the machine must have docker and docker-compose installed. Go to the root of the repository and execute appropriate commands. This will spin up the server and client containers along with the MongoDB container (if we are using local MongoDB server) inside of Docker environment.

Using docker containers with cloud hosted MongoDB

> cd project-root
> docker-compose -f docker-compose.mongocloud.yml build
> docker-compose -f docker-compose.mongocloud.yml up

Using docker containers with local MongoDB

> cd project-root
> docker-compose build
> docker-compose up

The client and server both are up and running and we should see the following screen if we navigate to the client url.

Login screen

  • seed data Go inside of the docker container and execute below commands. These will seed the database with roles, users and products data.

    docker exec -it appserver bash
    npm run db:seed
    npm run db:migrate

    You should see the following output: Product List Page

    Product List Page

    Product List Page

    You should be now login to the application and see the products list.

Visual Studio Code

Prerequisites

  • Node.js : To run npm packages
  • MongoDB : As a database for the application
Steps
  • To run via vscode, we should run the server and client side projects separately, and also make sure mongodb is up and running.

  • Create a .env file inside of the server directory. Add the below entries or change accordingly. You can follow the .env.sample file to see the format.

    DB_HOST=localhost
    DB_PORT=27017
    DB_NAME=appdb
    JWT_SECRET=secret
    JWT_EXPIRES_IN=3600
    PORT=5000
    IS_MONGODB_CLOUD_URL=false
    MONGODB_CLOUD_URL=mongodb+srv:// <USER >: <PASSWORD >@cluster0.abcd.mongodb.net/myFirstDatabase?retryWrites=true
    

Server commands

We assume we run the MongoDB in the docker container.

cd server
npm i
npm run db:up
npm start

Client commands

cd client-pro
nvm use --lts
yarn
yarn start

Data seeding

To seed the database, we need to run the following commands in the docker container. You can run these commands in the terminal as well if you are running the server and client outside of the docker environment. I assume we are running appserver in the docker container.

docker exec -it appserver bash
npm run db:seed
npm run db:migrate

You should be able to see the username and password in the /server/setup/users.json file.

Permission management ui

I have introduced a permission management ui for the application. Sample image is given below.

Product List Page

Centralized log to Sentry.io

We can also send the logs to sentry.io. To use this feature, we need to add the dsn entry into client/src/env.config.js. Setup snippet is like below in index.js file

Sentry.init({
  dsn: Config.dsn,
  integrations: [new Integrations.BrowserTracing()],
  tracesSampleRate: 1.0,
});

Test server API (NOT DONE YET)

To view the api, open your browser and visit http://localhost:5000/api-docs Ideally we should add all of the API endpoints into the swagger, but for the demo purpose, we only added Products API endpoint.
To test the APIs, we can reuse the postman collection. Open docs/rbac-mern-boilerplate.postman_collection.json into Postman and you should see the endpoints with appropriate payloads.

Scripts

Project Command Task
root npm run build Builds the containers
root npm run start Start the projects and database in docker containers
root docker exec -it appserver /bin/sh then npm run db:seed Executes seed products inside container
server npm i Install server side dependencies
server npm run db:up Start the database in a docker container
server npm run test Execute tests using jest
client npm i Install client side dependencies
client npm run start Start the react app
client npm run build Build the react app in production mode
client npm run test Execute tests using Testing Library

Types of testing

In this repository, we have included both unit testing and integration testing. For now, the code coverage is very low, but we are working on it to make it more than 90% somewhere in the future

Client side

  • We incorporated only unit tests in client side, and mock all the externals dependencies like localStorage and axios.

To run the tests, we can run npm run test command.

Server side

  • Integration testing: We have integrated the Jest as the testing library, and we have added supertest and mongodb-memory-server packages to make the integration testing easier.
  • Unit testing: We have used Jest to test the service layers and mock the external dependencies like MongoDB.

To run the tests, we can run npm run test command.

License

This project is MIT licensed.

Contribution

For now, I am not taking any community contritutions in terms of code. But if you have any suggestions or you found any bugs, please feel free to open an issue or a pull request.

On the other hand, if you want to know something, or want to start a discussion about this project, please start a discussion in our GitHub's discussion board.

Tutorials about how to build or use this project

I have been screen recording the coding steps of this project. You can find the videos in the YouTube playlist. These videos are only in Bangla, but I am happy to take special dedicated+paid sessions in English for international intersted audience. Feel free to reach out to me at [email protected] for any kind of help.

Thanks. Cheers.

More Repositories

1

mern-video-streaming

MERN Video Streaming is a cutting-edge, open-source platform for video streaming, offering a comprehensive, full-stack solution utilizing the latest MERN stack technologies.
JavaScript
94
star
2

ip-camera-browser-client

Normally, IP Camera streams video as RTSP protocol. But browser is unable to render this format, so we need to convert it as HLS format so that modern browser's video tags can display it.
JavaScript
68
star
3

rbac-react-redux-aspnetcore

A starter template for creating JWT token from ASP.NET Core API project and applying that JWT token authentication on React application
C#
63
star
4

linux-playbook-javascript

Linux software installation automation script (Linux playbook) using JavaScript and Google's ZX library.
JavaScript
49
star
5

bizbook-server

The repository of bizbook server web api project
C#
46
star
6

terminal-in-browser

This is an adventurous project where I aimed to input terminal commands in browser and execute them in server, and through websocket protocol show the actual terminal updates into the browser in real time.
TypeScript
38
star
7

ScreenshotTaker

This application takes the screenshot and send to the server. Useful if you have remote employee but don't want to use paid services like hubstaff
C#
36
star
8

parcelkoi

JavaScript
29
star
9

bizbook-client

The repository of bizbook client project
JavaScript
28
star
10

mock-interviews-2023

26
star
11

CodeCoopersLms

As Code Coopers Training (previously known as Virtual Tech Academy), we have launched a LMS for our students. We are open sourcing those LMS projects here one by one for the greater good. Please share and Mark Star if you appreciate it.
JavaScript
24
star
12

bizbook-server-dotnet-core

C#
22
star
13

learn-mern

JavaScript
20
star
14

botsapp

A bot builder on top of puppeteer's headless web browser mimicing your web.whatsapp.com functionalities.
JavaScript
20
star
15

memorizedquran

JavaScript
18
star
16

bizbook-client-angular

CSS
10
star
17

react-practices

JavaScript
10
star
18

OnlineCourse

This is a repository where upload the code shown in the recorded videos of mine.
PowerShell
9
star
19

code-coopers-lms

C#
9
star
20

bizbook365

C#
8
star
21

GenericComponents

Every application, let it be a desktop, web, mobile or any other you can image, either sends data to database or retrieves data from database to show it to the user. What if we automate the data sending and fetching process and focus only on the business logics? So that we don't need to write the common framework related hundreads of thousands lines of code for each of our projects, and the projects have been written all over the world?
C#
8
star
22

llm-pocs

JavaScript
7
star
23

ffmpeg-ui

UI for popular ffmpeg library
C#
7
star
24

rbac-typescript-node-react

TypeScript
6
star
25

VirtualTechAcademy

C#
5
star
26

mern-video-sharing

JavaScript
5
star
27

AspnetCoreAndReactB1

C#
4
star
28

video-overlays-obsolete

JavaScript
4
star
29

foyzulkarim

This repository is a special repository. Its README.md appears on my public profile!
4
star
30

no-code-poc

JavaScript
4
star
31

lms

JavaScript
3
star
32

academy-node-server

3
star
33

todoapp

JavaScript
3
star
34

SignalR

My R&D works on SignalR
JavaScript
3
star
35

PhoneChai

Phonechai.com development repository
C#
3
star
36

bizbook

2
star
37

clazzy

Class works of clazzy
JavaScript
2
star
38

faker-fastify-mongo

to be added later :)
JavaScript
2
star
39

bizbook-puppeteer

1
star
40

learn-mern-bangla-playlist

1
star
41

foyzul

CSS
1
star
42

react-5lcgln

Created with StackBlitz ⚑️
JavaScript
1
star
43

web-nvr-node-react

1
star
44

academy-react-client

1
star
45

commitstreams-ts

1
star