Simple example of NodeJS REST API for cat list
267
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
nodejs [v16]| Rest API call | CRUD operation | REST endpoints |
|---|---|---|
| GET | Read | http://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/PUT | Update) | http://0.0.0.0:8080/api/cats/{id} |
| POST {with body} | Create | http://0.0.0.0:8080/api/cats |
| DELETE | Delete | http://0.0.0.0:8080/api/cats/{id} |
Response Code | Response Status |
|---|---|
| 200 | OK |
| 201 | Created |
| 404 | Not Found |
Clone the repository on your machine
| Using | Comand |
|---|---|
| via https | git clone https://github.com/melnottp/cat-app-http.git |
| via ssh | git clone [email protected]:melnottp/cat-app-http.git |
cd cat-app-http
Prequisite / Assumption
docker installed and running on your machine.If not, its highly recomended to Get docker
docker build . -t cat-app-http
To know why we used
-t Allocate a pseudo-TTYread this stackoverflow thread
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
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
Lets hit the docker image as a client / User
| Test Type (Positive /Negative) | CLIENT On terminal | Response | SERVER (if Docker running with logs) |
|---|---|---|---|
Home Page | curl 0.0.0.0:8080 | Welcome, this is your Home page | CalledGET : / |
Invalid endpoint | http://0.0.0.0:8080/dascbajb | {"message":"Route not found"} | CalledGET : /dascbajb This endpoint is not implemented / unavailable at the moment !! |
health check | http://0.0.0.0:8080/health | {"uptime":29.560686169, "message":"OK","timestamp":1644057630652} | CalledGET : /health |
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 psit should show no running image
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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
Prequisite / Assumption
Kubernetes Cluster installed and running.To deploy the application
kubectl create deployment cat-app --image=tellmenop/cat-app-http:latest
cat-app-http was released under MIT License
Content type
Image
Digest
sha256:df10acd45…
Size
333.1 MB
Last updated
almost 4 years ago
docker pull tellmenop/cat-app-http