Using the power of npm modules

Back in Chapter 2, Creating Your First Johnny-Five Project, we used the color npm module to manage colors for us. We've used the barcli module to get our sensor data into bar graphs. Now it's time to use the request npm module to retrieve data from websites for us! This allows us to simplify development over microcontrollers that use C by not having to create HTTP requests by hand each time, and being able to use asynchronous calls.

For those unfamiliar with the request module, we'll use it to make HTTP GET requests like so:

const request = require('request')

request.get(url, (err, response, body) => {
console.log(body)
})

We give the request.get() call a URL and a callback that receives an error (that is, hopefully, null), a response object, and a body which is conveniently extracted for us from the full request object (which can be huge and complex).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174