Skip to content
This repository has been archived by the owner on Mar 17, 2020. It is now read-only.

spotguides/nodejs-mongodb

Repository files navigation

Spotguide for Node.js with MongoDB database

This repository was created by Banzai Cloud Pipeline. Spotguides provide automated configuration, logging, monitoring, security, and scaling for your application stacks.

Development

Every time you make changes to the source code and update the master branch, the CI/CD pipeline will be triggered to test, validate and update the deployment of your application. Check the .pipeline.yaml file for CI/CD steps.

Local development

Deploy to a local Kubernetes cluster

Requirements:

A local Kubernetes cluster must be running (eg. Docker for Desktop).

# verify the Kubernetes context
$ kubectl config get-contexts
# expected output
CURRENT   NAME                 CLUSTER                      AUTHINFO             NAMESPACE
*         docker-for-desktop   docker-for-desktop-cluster   docker-for-desktop
# build the Docker image and deploy via helm
$ cd .banzaicloud
$ skaffold config set --global local-cluster true
$ skaffold run

This will build the application and install all the components to Kubernetes.

Run tests

Using installed Node.js:

Requirements:

Commands:

# install dependencies
$ npm ci
$ npm test

Using Docker:

Requirements:

Commands:

docker build . --build-arg NODE_ENV=test -t spotguide-nodejs-mongodb
docker run -it --rm -v $(pwd):/home/node/ spotguide-nodejs-mongodb npm test

Start application in development mode

Uses nodemon, it is a utility that will monitor for any changes in your source and automatically restart your server.

Using local Node.js and MongoDB:

Requirements:

Commands:

# MongoDB must be running, set environment variables in .env or start dependencies
$ cat > .env <<'EOF'
# Used for local development only, when NODE_ENV=development
MONGODB_USERNAME=username
MONGODB_PASSWORD=password
MONGODB_DATABASE=spotguide-nodejs-mongodb
MONGODB_AUTH_SOURCE=admin
TRACING_DISABLED=true
EOF
$ docker-compose up -d db

$ npm install
$ npm run start:dev

Using Docker and Docker Compose:

Requirements:

Commands:

$ docker-compose up -d
# open http://127.0.0.1:3000/api/v1

Kubernetes ready Node.js

Our npm library provides all the essential features to prepare your Node.js application truly ready for production on Kubernetes, such as:

  • graceful error handling & shutdown
  • structured JSON logging
  • various HTTP middleware
  • health checks
  • metrics

Read more about it in our blog post.

Environment variables

name description default
PORT Application port 3000
MONGODB_URI MongoDB URI, scheme: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
MONGODB_HOST MongoDB host '127.0.0.1'
MONGODB_PORT MongoDB port 27017
MONGODB_USERNAME MongoDB user
MONGODB_PASSWORD MongoDB user password
MONGODB_DATABASE MongoDB database name

Endpoints

These are the implemented REST endpoints in the sample application.

GET /

Root endpoint, returns basic Pod information, like name, namespace and image.

GET /metrics

Prometheus metrics endpoint.

GET /healthy

Health check, liveness probe endpoint. Returns 200 when the application is healthy, can reach the database.

GET /ready

Readiness probe endpoint. Returns 200 when the application is ready to accept requests.

GET /api/v1/users

Fetch all users.

POST /api/v1/users

Create a new user. The request body has the following schema:

{
  "email": "",
  "username": "",
  "firstName": "",
  "lastName": ""
}

GET /api/v1/users/:id

Fetch a user.

PUT /api/v1/users/:id

Update a user. The request body has the same schema as POST /api/v1/users.

DELETE /api/v1/users/:id

Delete a user.