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.
- 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
- Use git clone to copy repo to local
git clone https://github.com/ThomasWYang/candidate_express_app.git
- Change directory into that folder and run following command, the app will run on port 4000.
node index.js
- Make sure Docker desktop (https://www.docker.com/) is running.
- Pull the docker image from docker hub.
docker pull thomasy2022/candidate_express_app
docker pull mongo
- Use docker-compose.yml file inside this repo combined with the docker image to run the app on port 4000
docker compose up
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)
- Show all candidates
curl -v "localhost:4000/candidates"
- Show single candidate by ID, if exist, that candidate will be returned.
curl "localhost:4000/candidates/1"
- Show single candidate by ID, if not exist, Not found info will be returned.
curl "localhost:4000/candidates/99"
- 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]”
- 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]\"}"
- 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}"
- 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}"
- Delete candidate with id
curl -v -X DELETE "localhost:4000/candidates/4"