Skip to content

Node + Typescript + Express + Sequelize + Mongoose + Validations + Socket + Docker + Swagger + Service Based Template

License

Notifications You must be signed in to change notification settings

Thre4dripper/NodeTs-Express-Service-Based-Template

Repository files navigation

NodeTs-Express-Service-Based-Template

Node TypeScript Express Sequelize MySQL PostgresSQL Sqlite MariaDB MSSql DB2 Snowflake Oracle Mongoose MongoDB Validations Socket Docker Swagger

A fully configurable Node.js, Express, and TypeScript server template with a service-based architecture that can interact with MySQL, PostgresSQL, Sqlite, MariaDB, MSSql, DB2, Snowflake, Oracle, MongoDB. Out-of-the-box validation, documentation generation, and more.

Features

  • Node.js, Express, TypeScript: Robust server setup using Node.js, Express, and TypeScript.
  • Sequelize: Integration with Sequelize for SQL database operations.
  • Mongoose: Integration with Mongoose for MongoDB database operations.
  • Database Compatibility: Interact with MySQL, PostgreSQL, MariaDB, Sqlite, MSSql, MongoDB.
  • Validation Mechanism: Pre-built validations for request payloads.
  • Automated Swagger Documentation: Automatically generated documentation available at /api-docs.
  • Service-Based Architecture: Modular approach for better organization and scalability.
  • Socket Events: Socket event handling using Socket.io.
  • Docker: Dockerized for easy deployment.

Modules

Automated Swagger Docs

  • Swagger documentation auto-generated for all routes.
  • Accessible at /api-docs.
  • Generated using the doc method in the MasterController class and Joi validation schemas.

MasterController (Heart of the application)

The MasterController is the backbone, providing functionalities for managing HTTP requests, socket events, payload validation, and more.

Features

  • Controller Logic Handling: restController method manages HTTP requests.
  • Socket Event Handling: socketController method manages socket events.
  • Payload Validation: joiValidator method validates incoming request payloads.
  • Swagger Documentation Generation: doc method generates Swagger documentation.
  • Route Handling: get, post, put, and delete methods register routes within the Express router.

Usage

Extend the MasterController to create controller classes for specific routes or socket events. Use the provided methods for efficient request handling, validation, and documentation generation.

Installation

Clone

Clone this repo to your local machine using `

$ git clone https://github.com/Thre4dripper/NodeTs-Express-Service-Based-Template

Server Setup

Install dependencies

$ npm install or yarn

Start the server

$ npm run dev or yarn dev

Build the project

$ npm run build or yarn build

Run the project

$ npm run start or yarn start

Database Setup

Additional dependencies are required for database operations. Install the appropriate dependencies for your database dialect.

MySQL

$ npm install mysql2

PostgreSQL

$ npm install pg pg-hstore

Sqlite

$ npm install sqlite

MariaDB

$ npm install mariadb

MSSql

$ npm install tedious or mssql

DB2

$ npm install ibm_db

Snowflake

$ npm install snowflake-sdk

Oracle

$ npm install oracledb

MongoDB

$ npm install mongoose

Docker

Docker Environment variables

  • PORT - Port number for the server to run on.
  • DB_DIALECT - Database dialect to use. (Options: mysql, postgres, mariadb, sqlite, mssql, mongodb)
  • DB_HOST - Database host.
  • DB_PORT - Database port.
  • DB_USER - Database username.
  • DB_PASS - Database password.
  • DB_NAME - Database name.
  • MONGO_URI - MongoDB URI (Only for MongoDB Dialect).
  • JWT_SECRET - Secret key for JWT.

Build the image

$ docker build -t <image-name> .

Run the container

$ docker run -e <env-variable>=<value> -p <port>:<port> <image-name>

Run the container in the background

$ docker run -d -e <env-variable>=<value> -p <port>:<port> <image-name>

Stop the container

$ docker stop <container-id>

Remove the container

$ docker rm <container-id>

Remove the image

$ docker rmi <image-name>

List all containers

$ docker ps -a

List all running containers

$ docker ps

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.