My First JavaScript REST API: Dog Facts Deployed To Heroku

Introduction

I recently deployed my first Javascript REST API that returns facts about dogs. The impetus behind creating this project was to get a working front end solution with the API providing the data. While working on the front end, …


This content originally appeared on DEV Community and was authored by mrsharm

Dogs

Introduction

I recently deployed my first Javascript REST API that returns facts about dogs. The impetus behind creating this project was to get a working front end solution with the API providing the data. While working on the front end, I started making use of the python API by DukeNgn that can be found here and ran into CORs issues that prevented me from getting the data.

Development

My main goal was to get the API deployed as quickly as possible and therefore, relied on the simplest solution making use of Express and the Javascript fs library to read a JSON file with all the facts. I also used the cors javascript library to make sure I wouldn't run into the same issue as the python library:

// App config.
const app = express();
app.use(express.json());
app.use(Cors());

All the code is available here.

Deployment with Heroku

The deployment to Heroku was smooth following this tutorial. The only issue I got into was hardcoding a port that wasn't agreeable with Heroku for which configuring the listening port accordingly did the trick:

const port = process.env.PORT || 5000;

One of the coolest Heroku features was to hook up my GitHub repository to Heroku resulting in an automatic deployment whenever I pushed changes.

Image description

Usage

Example

https://dogfacts-api.herokuapp.com/api/v1/resources/dogs?number=2

Output:

[
    {"fact":"Dogs are direct descendants of wolves."},
    {"fact":"Small quantities of grapes and raisins can cause renal failure in dogs. Chocolate, macadamia nuts, cooked onions, or anything with caffeine can also be harmful."}
]

Conclusion

Building the API was an awesome learning experience! Any feedback about the code or this blogpost would be greatly appreciated.


This content originally appeared on DEV Community and was authored by mrsharm


Print Share Comment Cite Upload Translate Updates
APA

mrsharm | Sciencx (2021-11-04T23:20:47+00:00) My First JavaScript REST API: Dog Facts Deployed To Heroku. Retrieved from https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/

MLA
" » My First JavaScript REST API: Dog Facts Deployed To Heroku." mrsharm | Sciencx - Thursday November 4, 2021, https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/
HARVARD
mrsharm | Sciencx Thursday November 4, 2021 » My First JavaScript REST API: Dog Facts Deployed To Heroku., viewed ,<https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/>
VANCOUVER
mrsharm | Sciencx - » My First JavaScript REST API: Dog Facts Deployed To Heroku. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/
CHICAGO
" » My First JavaScript REST API: Dog Facts Deployed To Heroku." mrsharm | Sciencx - Accessed . https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/
IEEE
" » My First JavaScript REST API: Dog Facts Deployed To Heroku." mrsharm | Sciencx [Online]. Available: https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/. [Accessed: ]
rf:citation
» My First JavaScript REST API: Dog Facts Deployed To Heroku | mrsharm | Sciencx | https://www.scien.cx/2021/11/04/my-first-javascript-rest-api-dog-facts-deployed-to-heroku/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.