As with all my projects where I am just testing something out for the first time, I created a folder called test_axios, cd into it, do an npm init, and install what I am testing out via npm install. In this test folder on top of using axios, I am also using cheerio to help work with html that I am grabbing at.
If you want you can also just clone in the demos I am making here with git as I have placed the test folder on my github account.
So you can just clone in the folder, cd into it, and run the basic demo I made like this:
For by basic example I am using axios to get the html of the webpage https://www.google.com, then loading the html into cheerio to help get the href attributes and inner text of each link.
So right off the bat axios strikes me as one of the best options for making get requests. That is pretty simple, and easy to remember. This is of course just a get request I am making for a public web page, but don’t worry when it comes to doing something more complicated everything I need is there.
So this is not something that I would use in production, just a custom hack job that I made while experimenting with axios, as I wanted something that I can run that will just respond to requests.
It’s a bit crude, but I just wanted something in place for this test project when it comes to using axios as a way to make requests from a browser.
For the purpose of this test project I quickly threw together a simple static website that I coded by hand. When the server script is started I can visit the site and test out my demos with my server.
The public folder just contains a main index.html, and two demos do far for making get requests, and post requests from the browser.
For a post request example I just made a project where I make a post request to the server, and get a response if the text matches a certain something.
Working with Url parameter is as simple as just giving an object with key value pairs rather than a sting that looks like this:
Although if you want to to it that way then it can just be appended to the url property given.
If you want a quick solution for parsing the url parameters back into an object on the server side check out url.parse in the built in node.js url module.
Axios is a good solution for making requests from a node.js, and browser environment. In the future I might update this post to cover some more advanced examples. This is a great project, it deserves a great post.