Creating a REST API

Turbo 360

A solid REST API is the foundation of virtually every modern web application, service and data-driven company or startup. From large multi-national corporation to small garage startup, the core tenets of a well-designed REST API are the same and can be learned in relatively short period of time. In this tutorial series, we construct a simple REST API using Node/Express, Mongo DB and deployed on Turbo 360. Along the way, we explore the key principles behind REST API design as well as common mistakes and scenarios that beginners often struggle with. Finally, we deploy our web service on the free Turbo 360 staging environment in conjunction with mLab for database hosting.


Project Setup

To start off the tutorial, we explore the basic concepts behind a REST API and explain the common use case scenarios. We then install the required tools and libraries necessary for the rest of the series and scaffold a base project to get started. Finally, we deploy the project on to a staging server to make sure it works both locally and on a live environment.

API Route

In this video, we connect the API route such that it returns data. We "seed" the project with static data using American football teams and players as examples. In doing so, we define our API resources and the underlying schema which will be used to organize the data in our database.

Mongo DB Connection

Now that our API endpoint is successfully connected, we get started on our Mongo DB by seeding the database with the list of players and teams we used in the previous video. We use the Mongo CLI to import each list from JSON files and then reconfigure our Node/Express project to connect to our Mongo DB locally.

Controllers

In this installment, we connect our API endpoints to our Mongo server in order to request resources from the actual database. We then remove the initial seed data and create database models for our REST API. Finally, we re-organize the api route to be more modular and extensible by using controllers to query for data rather than interfacing directly with the database models.

GET Requests

Now that our environment is properly set up and tested, it is time to design are web service according to REST design principles. We begin with GET requests which do exactly what they sound like - get data. We create endpoints for getting a set of data such as all players belonging to a certain team. Next, we create an endpoint for getting an individual piece of data using its unique identifier. We design are endpoints in compliance with standard REST architecture.

POST Requests

Now that our GET requests are properly set up, it is time to move on to POST. In a standard REST API, post requests are used to create new entries specified by resource. In this video, we create a POST endpoint which is used to create new teams and new players. We then test is by using the CURL function in our terminal to send post requests to our local server with JSON data for each new entry.

PUT Requests

According to standard REST API design structure, updating resources should happen via PUT requests. In this video, we set up a put endpoint for updating current entries in our database and test it by updating one of the players.

DELETE Requests

To finish out our CRUD implementation, we set up a DELETE request endpoint in order to remove entities from our REST API backend. We then test it using the curl function from the terminal and remove one player and one team from the database.

Deployment

We finish the series by deploying our project to a staging environment to make sure the endpoints work on a live server. In order to do so, we create a Mongo database on the mLab service (https://mlab.com) and configure our project to use the live database in a production capacity. Finally, we deploy the app to our staging environment on Turbo 360 and test the endpoints to confirm that they work the same way as they do locally.

Membership Options