Sign inSign up

tellmenop/cat-app-http

By tellmenop

•Updated almost 4 years ago

Simple example of NodeJS REST API for cat list

Image
0

267

tellmenop/cat-app-http repository overview

⁠cat-app-http

⁠Table of Contents

  1. What is cat-app-http⁠
  2. Project setup ⁠
  3. Running docker ⁠
  4. Running docker Image⁠
  5. Testing (is it working)⁠
  6. STOPPING docker (running container)⁠
  7. [Push docker image to repository]
  8. [Deploy to Kubernetes]
  9. MIT LICENSE⁠

⁠What is cat-app-http

It is a working project (non Prod ready) For getting started with a RESTFUL API server locally using docker⁠ The API is expose a list of cats with their characteristics

  • It runs a server (docker) using nodejs [v16]
  • Exposes following RESTFUL endpoints ( no database required) with all CRUD operations
Rest API callCRUD operationREST endpoints
GETReadhttp://0.0.0.0:8080/
http://0.0.0.0:8080/health
http://0.0.0.0:8080/api/cats
http://0.0.0.0:8080/api/cats/{id}
PATCH/PUTUpdate)http://0.0.0.0:8080/api/cats/{id}
POST {with body}Createhttp://0.0.0.0:8080/api/cats
DELETEDeletehttp://0.0.0.0:8080/api/cats/{id}
  • You may get 3 types of response
Response CodeResponse Status
200OK
201Created
404Not Found

⁠Project setup

Clone the repository on your machine

UsingComand
via httpsgit clone https://github.com/melnottp/cat-app-http.git
via sshgit clone [email protected]:melnottp/cat-app-http.git
cd cat-app-http

Prequisite / Assumption

  • You havedocker installed and running on your machine.

If not, its highly recomended to Get docker⁠

⁠Running docker

docker build . -t cat-app-http

To know why we used -t Allocate a pseudo-TTY read this stackoverflow thread⁠

⁠Verify docker Image

After docker build is completed, verify if a docker image is created and listed

run docker images

docker images
REPOSITORY                      TAG         IMAGE ID      CREATED         SIZE
localhost/cat-app-http  latest      8f74146744df  18 minutes ago  928 MB

You may have more than one row in result, but make sure you have the one with REPOSITORY localhost/cat-app-http

also see you got a random (uniqie) IMAGE ID assigned to the image you just created, in my case it was 8f74146744df

⁠Running docker Image

Now that you have a IMAGE ID, lets run that image

docker run -p 8080:8080 8f74146744df

docker run -p <your-port-external>:<docker-internal-port-exposed-for-access> IMAGE_ID

For more details on -p read **Publish or expose port (-p, --expose)**🔗⁠

open a new tab on terminal and verify this docker (running)

docker ps

⁠Testing

Lets hit the docker image as a client / User

Test Type (Positive /Negative)CLIENT On terminalResponseSERVER (if Docker running with logs)
Home Pagecurl 0.0.0.0:8080Welcome, this is your Home pageCalledGET : /
Invalid endpointhttp://0.0.0.0:8080/dascbajb{"message":"Route not found"}CalledGET : /dascbajb
This endpoint is not implemented / unavailable at the moment !!
health checkhttp://0.0.0.0:8080/health{"uptime":29.560686169,
"message":"OK","timestamp":1644057630652}
CalledGET : /health

⁠STOPPING docker (docker container)

firts lets find the runing one docker ps

CONTAINER ID  IMAGE                                  COMMAND      CREATED            STATUS                 PORTS                   NAMES
a5a149a53466  localhost/cat-app-http:latest  node app.js  About an hour ago  Up About a minute ago  0.0.0.0:8080->8080/tcp  ecstatic_cray

see the status column : STATUS

Up About a minute ago

Stop using 1.CONTAINER ID shell docker stop a5a149a5346

2.NAMES shell docker stop ecstatic_cray

In case you want to confirm ---->> run docker ps it should show no running image

docker ps
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

⁠Push docker image to repository

Generic command to push image to docker repository

docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname
sudo docker tag cat-app-http:latest tellmenop/cat-app-http:latest
sudo docker push tellmenop/cat-app-http:latest

⁠Deploy to Kubernetes

Prequisite / Assumption

  • You haveKubernetes Cluster installed and running.
  • Kubectl configured on your machine to access your Kubernetes Cluster

To deploy the application

kubectl create deployment cat-app --image=tellmenop/cat-app-http:latest

⁠License

cat-app-http was released under MIT License⁠

Tag summary

Content type

Image

Digest

sha256:df10acd45…

Size

333.1 MB

Last updated

almost 4 years ago

docker pull tellmenop/cat-app-http