This is a boilerplate/starter project for quickly building an express js server to build a RESTful APIs. It contains what I believe is a good app architecture should be. All suggestions are appreciated! Currently, it has boiler plate code for a 3 layer architecture. More can be found here about Express.js project architecture here on ι£δΉ¦.
By default, we use MySQL as the database. In this case, since we don't use an ORM, we can make calls directly in the service layer, therefore models/
is left blank.
The database used is MongoDB as it seems more popular in the Express/Node.js world. But if you want to run it, checkout an older readme.md at docs/readme_v1.md
In order to quickly run the project.
- Install the correct node version using
nvm use
- Install packages using
npm install
- Setup MySQL database server using
docker-compose --env-file .env -f docker/docker-compose-mongodb.yml up -d
, and create.env
file with DB information. Example shown below - Create a JWT token using
openssl rand -base64 32
or other methods.
PORT=8001
# MongoDB
MONGO_HOST="localhost"
MONGO_PORT="27017"
MONGO_USER="root"
MONGO_PASSWORD="root"
MONGO_DATABASE="test"
JWT_SECRET="secret"
JWT_EXPIRES_IN="1h"
- Run project using
npm run dev
.
The project hiearchy is shown below
src\
|--config\ # Environment variables and configuration related values
|--controllers\ # Route controllers (controller layer)
|--docs\ # Swagger files
|--models\ # Mongoose models (data layer)
|--schema\ # Contains Mongoose Schemas
|--routes\ # Routes
|--services\ # Business logic (service layer)
|--utils\ # Utility classes and functions
|--validations\ # Request data validation schemas
|--app.js # Express app entry
|--database.js # Database connection
|--middleware.js # Custom express middleware
tests\ # Contain tests
This sample API has one main resource/component, which has the following paths.
api/v1/auth/login # POST request
api/v1/auth/register # POST request
api/v1/auth/login_cache # GET request
api/v1/users/create_user # POST request
api/v1/users/get_user # GET request
api/v1/users/get_users # GET request
api/v1/users/update_user # PUT request
api/v1/users/delete_user # DELETE request
This boilerplate includes...
- 3 Layer Architecture
- Middleware Support
- JWT for Authentication
- Basic Unit and Integration Tests
- Github Workflow for CI/CD
- Data Safety - Password Hashing
- API Documentation with Swagger
- Simple Redis Login Counter Cache
Swagger documentation is set up at docs
. Remember to use authorization by copying value of authToken
from browser cookies to Authorize
button after /auth/login
or /auth/register
has been called.
Here are some more tips.
- Separate config files based on purpose.
db.config.js
for database configurement,thirdparty.config.js
for third party app configurement, etc... - Understand your data layer needs, comparing data driver (handwritten SQL queries) vs ORM models to define how
models/
should be used.
- AWS CloudFormation templating for easy aws deploy
- Pub/Sub Component
- Authorization/Validation
- Loaders logic seperation
- Seperate Model Logic from Controller