This is my entry for freeCodeCamp's fourth "APIs & Microservices" project. Demo is available on my site. You can also check out my other freeCodeCamp projects.
It was kind of challenging to find the user stories for this project, and all I could find is a forum link. I'm not sure if these are the actual user stories, but will go with these this time. 😅
- I can create a user by posting form data
usernameto/api/exercise/new-userand returned will be an object withusernameand_id. - I can get an array of all users by getting
api/exercise/userswith the same info as when creating a user. - I can add an exercise to any user by posting form data
userId(_id),description,duration, and optionallydateto/api/exercise/add. If no date supplied it will use current date. Returned will be the user object with the exercise fields added. - I can retrieve a full exercise log of any user by getting
/api/exercise/logwith a parameter ofuserId(_id). Return will be the user object with added arraylogandcount(total exercise count). - I can retrieve part of the log of any user by also passing along optional parameters of
from&toorlimit. (Date formatyyyy-mm-dd, limit =int)
In the following example we create a new user, add a new activity for her and fetch all her activities.
fetch('/api/exercise/new-user', {
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: JSON.stringify({ username: 'Sara' }),
})
.then(res => res.json())
.then(user =>
fetch('/api/exercise/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: JSON.stringify({
description: 'runnning',
duration: 45,
userId: user._id,
}),
})
)
.then(res => res.json())
.then(({ user }) => fetch(`/api/exercise/log/?userId=${user}`))
.then(res => res.json())
.then(console.log)
.catch(console.error);{
"_id": "5b896ce5de10b36f64307ebb",
"username": "Sara",
"count": 1,
"log": [
{
"_id": "5b896ce5de10b36f64307ebc",
"date": "2018-08-31T16:29:25.093Z",
"description": "runnning",
"duration": 45
}
]
}| Method | Route | Description |
|---|---|---|
| GET | /api/exercise/users | Get an array of all users |
| GET | /api/exercise/log | Retrieve a full exercise log of any user |
| POST | /api/exercise/add | Add an exercise to any user |
| POST | /api/exercise/new-user | Create a new user |
- ESLint linter with Airbnb's base config
- Express.js framework
- Jest test framework
- Pug template engine
- Supertest library
git clone https://github.com/zsoltime/fcc-api-exercise-tracker.git
cd fcc-api-exercise-trackernpm installIt starts a dev server, monitor for changes and restarts on any change.
npm run devIt starts the node.js application.
npm startIt runs tests using Jest and Supertest.
npm test