Created by: MrDay2Day
Node Version: v22.6
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.
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.
- Backblaze π
- AWS SES π
- ClickSend (Maybe)
- MongoDB ππ
- MySQL π
- PostGreSQL π
Redis-> KeyDB/ValKey (Pub/Sub implementation ONLY)
- ExpressJS π
- Kafka π¨
- Multer π
- Socket.IO π
- Cors
β οΈ - Compression
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.
- TypeScript for faster development.
- Self generating SQL databases and tables.
- WebSocket Enabled via SocketIO which also enables scalability through
Redis-> KeyDB/ValKey using websocket connections. - Using multiple database concurrently.
- File management.
- Docker Enabled.
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
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
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
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
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
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
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
To adjust cluster size for deployment using Docker or PM2 edit .env
file
CLUSTER_SIZE=5
This will deploy a cluster with 5 nodes.
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. |
These are features & technologies that may or may not be integrated in the future.
Icon | Status |
---|---|
β | Completed |
β‘οΈ | In Progress |
βοΈ | Planning |
π | Considering |
Issue | |
β | Cancelled |
Icon | Implementation |
---|---|
π‘ | Custom Implementation (Custom implementations with docs & examples) |
π―οΈ | Standard/Traditional Implementation |
β | Not Sure |
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. |
β | π‘ | 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 |
I am under the mindset that payments management should be implemented on a per need basis based on your application.
Plans to implemented as a micro-service however I would recommend using Logto.