Skip to content

This is a simple Express REST server with with SocketIo distributed through Redis/KeyDB, and Database integration MySQL, PostGresSQL and MongoDB, and Server-side Events.

License

Notifications You must be signed in to change notification settings

MrDay2Day/NodeJs_Express_Server_Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

59 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Β 
Β 

Created by: MrDay2Day
Node Version: v22.6

What is Repo this?

Short answer: A Boilerplate NodeJS Express Server Template written in Typescript built for scale!

The not so short answer: This is a work-in-progress boilerplate Typescript written Express REST Server with integrated WebSocket & Redis -> KeyDB/ValKey. This template is design to operate at scale using Redis -> KeyDB/ValKey and can easily be deployed as a node cluster or/and on separate machines locally or/and globally.

This template has built-in file management using S3 (Simple Storage Service) protocol and an emailing engine that can send raw html emails with attachments through AWS SES (Simple Emailing Service).

These are all custom solutions and are at no point in time a RULE that must be followed, you are welcome to make changes as you see fit.

Why did I build Repo?

Simple I am a developer who creates a lot of servers & micro-services and I often find myself copying and pasting from previous projects the same code. So I said to myself why not just create a general template that has all the things I use most in my favorite worse language Javascript with a bit of seasoning being Typescript.

So I've integrated some 3rd party packages and services which I know a lot of people will love and also so honorary mentions from my favorites' list.

Services

  • Backblaze πŸ†
  • AWS SES πŸ†
  • ClickSend (Maybe)

Databases

  • MongoDB πŸ†πŸ†
  • MySQL πŸ†
  • PostGreSQL πŸ†
  • Redis -> KeyDB/ValKey (Pub/Sub implementation ONLY)

Other Packages

  • ExpressJS πŸ†
  • Kafka πŸ’¨
  • Multer πŸ†
  • Socket.IO πŸ†
  • Cors ⚠️
  • Compression

Why recommend KeyDB/ValKey > RedisπŸ–•πŸΏ

Well, that is because of the whole license issue and performance. In multiple test KeDB out performed Redis by a significant amount. KeyDB/ValKey might not be fully up-to-date with Redis 7 however it still has most of the main features of Redis to be used as an alternative for cache store, database & Pub/Sub(amongst other key features).

You are still able to use Redis if it and it's license doesn't affect your needs.

Key Features

  1. TypeScript for faster development.
  2. Self generating SQL databases and tables.
  3. WebSocket Enabled via SocketIO which also enables scalability through Redis -> KeyDB/ValKey using websocket connections.
  4. Using multiple database concurrently.
  5. File management.
  6. Docker Enabled.

Development Environment Setup

Copy and paste .env.template to .env

cp .env.template .env

Then edit the .env file with the necessary credentials. Input the required information for the services/features/libraries which will be used.

You are able to use multiple databases in this template whether individually or all at once PostGres, MySQL and MongoDB.

You are able to auto create Database and Tables/Collections on the fly when server starts up. NB: Database can be activated of deactivated based on the *_ACTIVE (* = MONGO | PG | MYSQL) variable y is for active, leave blank if not active eg: MYSQL_ACTUVE=y.

After Environment variables are setup in the .env file you need to do a ci(Continuous Integration) installation of the node_modules:

npm ci

Local Development

To start server in development mode and compile to vanilla Javascript then run with nodemon use:

npm run dev

To run Typescript using ts-node:

npm run ts-dev

Using Docker for development.

This template allows you to use Docker for both development and production. As a developer you are free to make any changes you see fit to customize the development environment. If you do not know what you are doing do not edit any changes.

To start development in Docker you can use the shell script, this starts the development server in docker using user Node v22.6 with ts-node executing Typescript.

./app.sh docker-dev

To user nodemon while compiling Typescript to Javascript

./app.sh docker-dev-node

Production Setup

Local Production Deployment

Deploying on server the simple way.

npm run start

This does a Typescript compilation then executes the Javascript code via 'code/server.js'

If you have any issues as it relates to type errors can also do a manual compilation by using:

npm run tsc

then:

npm run start-server

The npm run start-server command runs the 'code/server.js' directly

Docker Production Deployment

To do a production deployment using Docker, use the app.sh script. A simple deployment is as follows:

./app.sh docker-prod

This will deploy a single container of the server.

You are able to deploy as a cluster for availability and load balance using nginx. The environment variable CLUSTER_SIZE is use to determine the cluster's size. To Deploy use the following script:

./app.sh docker-cluster

PM2 Production Deployment

To deploy using PM2 is simple just use the following script:

./app.sh pm2-deploy

This will deploy a PM2 cluster of the server. To restart the cluster simple use:

./app.sh pm2-restart

To stop the cluster you just need to run

./app.sh pm2-stop

Changing Node Version

To change Node version ensure your change the version in package.json:

{
  "engines": {
    "node": "22.6" << Change here
  },
  ...
}

..and also the Node version in the docker-compose-*.yml files are controller by the .env file:

PORT=3330

Adjusting Cluster Size

To adjust cluster size for deployment using Docker or PM2 edit .env file

CLUSTER_SIZE=5

This will deploy a cluster with 5 nodes.

Utility Helper Codes

Title Code Example Description
Random Number Generator getRandomNumber(number, number) Takes in a high and low number and returns a random number in-between both.
Random String Generator generateString(length) Great for creating temporary passwords with uppercase, lowercase, number & special character.
JSON Data Checker checkJSONToData<DataType>(JSON) Check if a string is JSON and if it is the expected type structure.
Random select from Array getRandomElement<ArrayDataType>(array) Randomly select an element from an array.
String to Number Converter isStringNumber(string) Converts a string to a number if possible.

Feature & Technologies

These are features & technologies that may or may not be integrated in the future.

Stages πŸ‘‡πŸΏ

Icon Status
βœ… Completed
➑️ In Progress
✏️ Planning
πŸ’­ Considering
⚠️ Issue
❌ Cancelled

Implementation Style πŸ‘‡πŸΏ

Icon Implementation
πŸ’‘ Custom Implementation (Custom implementations with docs & examples)
πŸ•―οΈ Standard/Traditional Implementation
❓ Not Sure

Current List

Status Feature Implementation Notes
βœ… MySQL πŸ’‘ Industry "go to" for database.
βœ… PostGrSQL πŸ’‘ Large community of extension.
βœ… MongoDB πŸ’‘ Recommended DB for Scale and large amount of data.
βœ… Socket.IO πŸ’‘ Realtime Communication between client and server.
βœ… Redis -> KeyDB/ValKey πŸ’‘ Pub/Sub implementation ONLY used for cluster communication, can be used as DB cache with your own implementation.
βœ… Multer πŸ’‘ File Management for form-data uploads.
βœ… Backblaze πŸ’‘ S3 Storage - Private & Public.
βœ… Dockerize πŸ•―οΈ For Development and Production Deployment.
βœ… Custom Task Queue / CRON / Email with AWS-SES πŸ•―οΈ Implemented however I created a mico-service repo for when deploying at scale.
βœ… ts-node/ts-node-dev πŸ’‘ TypeScript execution engine.(Strict type requirements)
➑️ ServerSide Events with Example πŸ’‘ To be distributed through data stream for deployment at scale.
➑️ Kafka πŸ’‘ Data Streaming.
✏️ SMS πŸ•―οΈ Unable to find a suitable provider.
❌ tRCP
❌ GraphQL
❌ HandlebarsJS with Examples

Payments πŸ’­

I am under the mindset that payments management should be implemented on a per need basis based on your application.

Authentication ⚠️

Plans to implemented as a micro-service however I would recommend using Logto.

About

This is a simple Express REST server with with SocketIo distributed through Redis/KeyDB, and Database integration MySQL, PostGresSQL and MongoDB, and Server-side Events.

Topics

Resources

License

Stars

Watchers

Forks