Setting up Axios
In this lesson, we're going to set up axios in our project
Setting up axios#
We did work with axios in the beginning, but I want to set it up correctly. It's better to locate it at a different directory and then use it from different components, so let's do just that.
src directory, let's create a new folder called
actions. Inside the actions folder, we need to make a file called
agent.ts. This file will be responsible for all the API requests we make to our server.
First thing we need to do is import axios from axios. Also, in axios, we can store base url in a variable, so it gets easier if we want to append anything on it. We already know what our base url is, we just need to write:
axios.defaults.baseUrl = 'http://localhost:5000/api';
We can also save our responseBody in a variable. If you remember, our data is stored inside
response.data, so we do just that to make our lives a little bit easier.