Context
NPM packages, making requests with Axios
Up to this point we have used only node.js core modules to achieve our goals and accomplish our tasks. But what if we want to do more than simply handling things within the filesystem?
What if we want to get data from another server, connect to a database, store and retrieve information from there, connect to sensors and many many more. Then although it is possible to do everything within the strict node.js core, it will take time and effort, and it doesn't make any sense to reinvent the wheel.
That's why we will use npm registry again to install external packages that will take a lot of development pain away from us. If you don't remember what npm registry is or how to install packages as dependencies, or dev-dependencies you are very welcomed to visit the react.js part and learn more about this there. NPM is the cornerstone of managing JavaScript modules (equivalent to anaconda for python, or the gem store for ruby if you are familiar).
In our next task we will focus on creating a script that makes requests to public api's and brings data from there. Exactly like we did it with the fetch API when we were working on the browser. With node.js we will not use the fetch API though (as it is solely browser oriented solution) but a package that is called axios and can perform many http requests exactly like the fetch, inside and outside the browser.
Then, after we have installed axios package, and imported it into our script, we can start making requests to api's by parameterizing the options of each request (request's method, url endpoint, data to send etc.) exactly like we did with fetch.
If you want to learn more about the axios api and the possible functions it provides, check the docs here.
In our case we will use axios to make a get request to our usual openweathermap api and bring data for a given city. We will use the same url endpoints as when we were working on the weather app during our front end projects. In this case as you can see above, we ask for the current weather and more precisely for the temperature of the day for Hamburg, Germany. Then we print this sliced piece of info to the console.
Piece of cake, right?