REST API Project: Exercise Tracker

Libor Marko
3 min readDec 21, 2019

--

API is the acronym for Application Programming Interface that allows two applications to talk to each other. REST determines how the API looks like. It stands for Representational State Transfer. It is a set of rules that developers follow when they create their API. I created this simple REST API Exercise Tracker project, which you can use e.g. to track your fitness progress. You can try the project here, see the code in this GitHub repository or on my Glitch profile.

Technologies I used

  • Node.JS
  • Express
  • REST API
  • JSON
  • npm
  • JavaScript
  • HTML5
  • CSS3

How it works

The app’s code contains many comments that explain each individual part of the app into detail. Here I summarize the main app’s functionality and structure.

The app stores data in a database. The model and the schema userSchema, which allows creating an instance of individual documents are stored in the app’s folder called models. A user can store data such as a username and exercise log, including description, duration, and date.

Firstly, a new user has to be created by submitting a requested username. The app uses the POST method and validates the name’s uniqueness by communication with the database. In a case of no error, the username is stored in the DB and not available anymore. A username gets back in JSON response, together with an additional id number.

Once a username is stored, we can start with logging exercises using the POST method. We need a unique user’s id and desired data like description, duration, and date; written in a valid format. We submit data, the app finds the user in the database, and updates data using findOneAndUpdate() method. As a response, we get back the JSON data summary of what we submitted. If the submitted date is empty, the app uses the current date.

We can visit the path [app’s URL]/api/exercise/users, and use the defined GET method to get data about all users. The method responds with data in JSON format showing individual usernames and corresponding ids.

There is defined also the GET method on the path
[app’s URL]/api/exercise/log?userId=a user&from=from date&to=to date.
We choose a user by the corresponding id, and optionally we choose from and to dates. In response, we get back the complete exercise log of the user. The app's logic handles all possible chosen scenarios within from and to dates. We can choose none of the dates, one of them, or both of them.

--

--

Libor Marko

Senior FrontEnd Engineer. Follow me on LinkedIn or check my web page: http://libormarko.github.io