This week in your project you will be making API calls from the backend. In the past few weeks you have been making API calls from the frontend with the XMLHttpRequest object and it's methods. Now that you are in the backend we would like you to use the Request module. This workshop is intended to familiarise you with this module.
- 50 mins to complete the challenge in pairs
- 10 minutes to regroup and talk over the solution
To become familiar with the Request module and the Node core http module.
The Request is a very popular module that helps to make HTTP requests as simple as possible and it is built around the Node HTTP core module. Here is a brilliant article on the request module.
Example 1 - I would like to GET my profile-photo.png from the database to display on my homepage.
Example 2 - I would like to use the Request module to make a HTTP request to the TFL API, to find out which busses go between London Bridge and Bethnal Green.
Create a function called myRequest that has similar set of features to the Node request module.
The request module simply creates a wrapper around the Node core http module. Your task it to create your own request function that performs in the same way as the request module used in the app.js file in this repo. Hint: relate to your knowledge of streams and sending data in chunks.
To complete this challenge you will need to use the http.get
method of the node core http
module.
- Clone this repo & run
npm install
- Run the app.js file in your command line with the command
node app.js
- Open app.js and follow the instructions to create your own request function
Can you alter your myRequest function to check which protocol the url is using (http or https) and make the suitable request. You will need to require in the https
node core module. You can test this by using the https://jsonplaceholder.typicode.com/users/1
url.