https://github.com/spotguides/nodejs-mongodb
Spotguide for Node.js with MongoDB database
https://github.com/spotguides/nodejs-mongodb
docker kubernetes mongodb-database monitoring nodejs spotguide
Last synced: 9 months ago
JSON representation
Spotguide for Node.js with MongoDB database
- Host: GitHub
- URL: https://github.com/spotguides/nodejs-mongodb
- Owner: spotguides
- License: apache-2.0
- Archived: true
- Created: 2018-11-29T14:30:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-17T10:15:37.000Z (over 6 years ago)
- Last Synced: 2024-09-27T17:21:37.442Z (almost 2 years ago)
- Topics: docker, kubernetes, mongodb-database, monitoring, nodejs, spotguide
- Language: JavaScript
- Homepage: https://banzaicloud.com/blog/spotguides-revisited/
- Size: 1.12 MB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# 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.
- [Spotguide for Node.js with MongoDB database](#spotguide-for-nodejs-with-mongodb-database)
- [Development](#development)
- [Local development](#local-development)
- [Deploy to a local Kubernetes cluster](#deploy-to-a-local-kubernetes-cluster)
- [Run tests](#run-tests)
- [Start application in development mode](#start-application-in-development-mode)
- [Kubernetes ready Node.js](#kubernetes-ready-nodejs)
- [Environment variables](#environment-variables)
- [Endpoints](#endpoints)
- [`GET /`](#get)
- [`GET /metrics`](#get-metrics)
- [`GET /healthy`](#get-healthy)
- [`GET /ready`](#get-ready)
- [`GET /api/v1/users`](#get-apiv1users)
- [`POST /api/v1/users`](#post-apiv1users)
- [`GET /api/v1/users/:id`](#get-apiv1usersid)
- [`PUT /api/v1/users/:id`](#put-apiv1usersid)
- [`DELETE /api/v1/users/:id`](#delete-apiv1usersid)
## 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`](.banzaicloud/pipeline.yaml) file for CI/CD steps.
### Local development
#### Deploy to a local Kubernetes cluster
_Requirements:_
- [Docker](https://www.docker.com/)
- [Docker for Desktop](https://www.docker.com/products/docker-desktop)
- [skaffold](https://github.com/GoogleContainerTools/skaffold) and its dependencies
A local Kubernetes cluster must be running (eg. [Docker for Desktop](https://www.docker.com/products/docker-desktop)).
```sh
# 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:_
- [Node.js](https://nodejs.org/)
_Commands:_
```sh
# install dependencies
$ npm ci
$ npm test
```
Using Docker:
_Requirements:_
- [Docker](https://www.docker.com/)
_Commands:_
```sh
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](https://nodemon.io/), 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:_
- [Node.js](https://nodejs.org/)
- [MongoDB](https://www.mongodb.com/)
_Commands:_
```sh
# 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:_
- [Docker](https://www.docker.com/)
_Commands:_
```sh
$ docker-compose up -d
# open http://127.0.0.1:3000/api/v1
```
### Kubernetes ready Node.js
Our [`npm` library](https://github.com/banzaicloud/node-service-tools) 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](https://banzaicloud.com/blog/nodejs-in-production/).
## 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](https://kubernetes.io/docs/concepts/workloads/pods/pod/) information, like name, namespace and image.
### `GET /metrics`
[Prometheus](https://prometheus.io) 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:
```json
{
"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`](#post-apiv1users).
### `DELETE /api/v1/users/:id`
Delete a user.