Backend API for the Todo List example app.
👋 Welcome! This example app is part of this article in the docs for KintoHub to show how you can build cloud native apps in seconds. Head over there to read the guided steps to use this project.
This Node.js based backend API app has a dependency on a MongoDB database. It's meant to be used with the Frontend app.
The app uses the hapi framework for handling API routes. It exposes a REST API for persisting Todo items, since the frontend app is based on TodoMVC and relies on this backend for storing the todos.
The REST API has apiDoc annotations, refer to those for usage in app/index.js
.
For the purpose of easy debugging, the app logs to the console all API calls and related output.
The only thing you must specify is the environment variable that has a connection string to point to the MongoDB database.
MONGODB_CONNECTION_STRING
(required) – MongoDB connection string, e.g.mongodb://<user_name>:<password>@localhost:27017/<db_name>?authSource=<auth_db_name>
SERVER_HOST
(optional) – host where the server is bound to. Defaults to0.0.0.0
(all available network interfaces) if undefined.SERVER_PORT
(optional) – port where the server listens on. Defaults to8000
if undefined.
You'll need to have a MongoDB database. You could install MongoDB, or if you have Docker Desktop installed, you can spin up a temporary one quickly.
- Once Docker Desktop is installed, you can open create a new MongoDB container:
It will run (detached) a MongoDB 4.0 container
docker run --name todolist-mongo -d -p 27017:27017 mongo:4.0
todolist-mongo
and exposes the default MongoDB port on your localhost. - To confirm whether the container is running, run:
docker ps
- Once you're done, you can quit Docker Desktop or run:
docker stop todolist-mongo
- When you get going again, start up Docker Desktop and run:
docker start todolist-mongo
To make it easier to specify environment variables for local development, the app uses the dotenv node module to load environment variables from file. You can refer to the example file .env.example
, and copy it to a new .env
file in the root folder. It's ignored in git.
If you use the MongoDB with Docker method, set your .env
file like this:
MONGODB_CONNECTION_STRING=mongodb://localhost:27017/todolist
- Install dependencies:
npm install
- Start in debugging mode:
npm start
For running MongoDB in production, check out the related KintoHub docs article how you can quickly get a cluster going with Helm charts, so it has automatic failover built-in. (you shouldn't run just a single MongoDB instance in a container for production workloads)
Ensure the environment variable MONGODB_CONNECTION_STRING
is set, and point to your production grade MongoDB cluster.
You may also want to set the correct SERVER_PORT
for exposing the service.
- Build the code for a production release:
npm run build
- Serve the built application:
npm run serve
🚀 Deploying on KintoHub
Thank you for checking out this example app. KintoHub makes it super easy to build, deploy, manage and operate your cloud native apps, like this one.
Find out more at www.kintohub.com.