Skip to content

ThomasWYang/candidate_express_app

Repository files navigation

Readme

This is a simple Rest Api created using Node and Express JS with local Mongo DB. The object Model is Candidate with fname(string), lname(string), email(string) and score(int). When the Api started, it will preloaded with 4 Candidates with predefined names and random scores from 60 to 100.

Install via Git

  1. Make sure Node.js 18+ (https://nodejs.org/en/) and MongoDB (https://www.mongodb.com/try/download/community) is installed in the local environment. When install MongoDB, Select "Run the service as Network Service user (Default)" and "Installed MongoDB as a Windows Service". Use following command in command line to check if node is installed.
node -v
npm -v
  1. Use git clone to copy repo to local
git clone https://github.com/ThomasWYang/candidate_express_app.git
  1. Change directory into that folder and run following command, the app will run on port 4000.
node index.js

Install via Docker

  1. Make sure Docker desktop (https://www.docker.com/) is running.
  2. Pull the docker image from docker hub.
docker pull thomasy2022/candidate_express_app
docker pull mongo
  1. Use docker-compose.yml file inside this repo combined with the docker image to run the app on port 4000
docker compose up

Usage

You may test the script via various tools like browsers and postman, I will show how to use curl to test the api in windows cmd or linux below. (add -v after curl to show detailed info)

  1. Show all candidates
curl -v "localhost:4000/candidates"
  1. Show single candidate by ID, if exist, that candidate will be returned.
curl "localhost:4000/candidates/1"
  1. Show single candidate by ID, if not exist, Not found info will be returned.
curl "localhost:4000/candidates/99"
  1. Search candidates by fname, lname or email (you can give any combination of these 3 fields).
curl “localhost:4000/candidates?fname=Thomas&lname=Yang”
curl “localhost:4000/candidates?[email protected]
  1. Create candidate with fname, lname and email (id will be generated automatically)
curl -v -X POST "localhost:4000/candidates" -H "Content-type:application/json" -d "{\"fname\": \"aaa\", \"lname\": \"bbb\", \"email\":\"[email protected]\"}"
  1. Create candidate with fname, lname, email and score (id will be generated automatically)
curl -v -X POST "localhost:4000/candidates" -H "Content-type:application/json" -d "{\"fname\": \"mmm\", \"lname\": \"nnn\", \"email\":\"[email protected]\", \"score\":80}"
  1. Update candidate with id
curl -v -X PUT "localhost:4000/candidates/4" -H "Content-type:application/json" -d "{\"fname\": \"xxx\", \"lname\": \"yyy\", \"email\":\"[email protected]\", \"score\":90}"
  1. Delete candidate with id
curl -v -X DELETE "localhost:4000/candidates/4"

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published