Skip to content

Latest commit

 

History

History
208 lines (190 loc) · 5.7 KB

README.md

File metadata and controls

208 lines (190 loc) · 5.7 KB

Project template for future microservices in Node.js.

Version: 1.0.1


Steps to run this project in development environment:

  1. Run npm i command
  2. Export required env vars
export NODE_ENV='development'
export JWT_SECRET='not so super secret content'
export USER_SERVICE_URL='http://local-user-service-url'
export DB_USER='root'
export DB_PASSWORD='1234'
export REDIS_PASSWORD='1234'
export DOC_PATH='explorer'
export EXPOSE_SWAGGER_UI='true'
export EXPOSE_API_DOCS='true'
export API_PROTOCOL='http'
export API_HOSTNAME='localhost'
export API_PORT='80'
export API_PREFIX=''
  1. Add the following content to CONFIG env var for the development environment
{
  "database": {
    "type": "sqlite",
    "database": "./database.sql"
  },
  "redis": {
    "port": 6379,
    "host": "localhost"
  }
}

Example using quotes:

export CONFIG='{
  "database": {
    "type": "sqlite",
    "database": "./database.sql"
  },
  "redis": {
    "port": 6379,
    "host": "localhost"
  }
}'
  1. Run npm start command

Steps to run in production environment using npm:

  1. Run npm i command
  2. Run npm run build command
  3. Run npm ci --production command
  4. Export required env vars
export NODE_ENV='production'
export JWT_SECRET='super secret content'
export USER_SERVICE_URL='http://user-service-url'
export DB_USER='admin'
export DB_PASSWORD='MDBP@sWoRd'
export REDIS_PASSWORD='R3d1sP@sWoRd'
export DOC_PATH='explorer'
export EXPOSE_SWAGGER_UI='false'
export EXPOSE_API_DOCS='true'
export API_PROTOCOL='https'
export API_HOSTNAME='node-service.com'
export API_PORT='80'
export API_PREFIX='service'
  1. Add the following content to CONFIG env var for the production environment
{
  "database": {
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "database": "db_name"
  },
  "redis": {
    "port": 6379,
    "host": "localhost"
  }
}

Example using quotes:

export CONFIG='{
  "database": {
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "database": "db_name"
  },
  "redis": {
    "port": 6379,
    "host": "localhost"
  }
}'
  1. Run node build/src command

Steps to run in production environment using Docker:

  1. Run the following command to build the Docker Image
docker build -t <image-name>:<tag> .
  1. Set the CONFIG env var for production environment
export CONFIG='{
  "database": {
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "database": "db_name"
  },
  "redis": {
    "port": 6379,
    "host": "localhost"
  }
}'
  1. Run the following command to run a Docker Container
docker run -dp <port>:80 \
  -e NODE_ENV='production' \
  -e JWT_SECRET='super secret content' \
  -e USER_SERVICE_URL='http://user-service-url' \
  -e DB_USER='root' \
  -e DB_PASSWORD='1234' \
  -e REDIS_PASSWORD='1234' \
  -e CONFIG \
  -e DOC_PATH='explorer' \
  -e EXPOSE_SWAGGER_UI='false' \
  -e EXPOSE_API_DOCS='true' \
  -e API_PROTOCOL='https' \
  -e API_HOSTNAME='node-service.com' \
  -e API_PORT='80' \
  -e API_PREFIX='service' \
  <image>:<tag>

Steps to run in any environment using Docker Compose:

  1. Configure docker-compose.yml to suit the environment needs
  2. Run any of the following commands
# using old docker-compose CLI
docker-compose up
# using new built-in docker compose CLI
docker compose up
# run in a detached mode
docker compose up -d

Steps to run inside a Kubernetes cluster using kubectl:

  1. Configure kubernetes.yml to suit the environment needs
  2. Run the following command(s)
# run it inside the default namespace
kubectl apply -f kubernetes.yml

# run it inside a separate namespace
kubectl create namespace <namespace-name>
kubectl apply -f kubernetes.yml -n <namespace-name>

Used environment variables

Env variable Description Example Default
PORT HTTP server listen port 3000 80
NODE_ENV Node configuration environment production/development development
DOC_PATH Path of Swagger API doc api-docs-ui explorer
JWT_SECRET JWT Secret 5IUSD123JKJASA TOKEN
USER_SERVICE_URL URL of remote service http://url-to-service null
DB_USER Database user admin root
DB_PASSWORD Database password pwd123 null
REDIS_PASSWORD Redis password redpwd123 null
API_PROTOCOL Protocol of API server https http
API_HOSTNAME Hostname of API server node-service.com localhost
API_PORT Port of API server 3000 80
API_PREFIX Path prefix of API server nodeService /
CONFIG Additional configuration in JSON string format {database: {...}, redis: {...}} null

Useful documentation links for third party libs.

Lib Description URL
TypeScript Statically typed language on top of JavaScript. https://www.typescriptlang.org/docs/
Inversify.js IoC container for JavaScript. https://inversify.io/
socket.io WebSocket lib for JavaScript. https://socket.io/
Redis JavaScript client lib for Redis. https://redis.io/
Express Node.js HTTP server. https://expressjs.com/
Swagger JSDoc API doc generator. https://brikev.github.io/express-jsdoc-swagger-docs/
Mocha JavaScript testing library. https://mochajs.org/
Chai JavaScript assertion library. https://www.chaijs.com/
Sinon Standalone test spies, stubs and mocks for JavaScript. https://sinonjs.org/
Express Validator Validation middleware for Express. https://express-validator.github.io/docs/