We use axios for http requests such as get, post, etc. We use express for the same purpose also. However according to what I read, they are for different purposes. Please explain how.
PS: If you explain it by giving an example, it would be great!
We use axios for http requests such as get, post, etc. We use express for the same purpose also. However according to what I read, they are for different purposes. Please explain how.
PS: If you explain it by giving an example, it would be great!
You can think of express.js as a warehouse:
app.get('/item/:name', async function (req, res) {
res.send(await findItemByName(req.params.name));
});
If you want to get an item, for example a pencil
, from this warehouse, you can use axios.js.
axios.get('/item/pencil')
Axios is used to send a web request whereas express is used to listen and serve these web requests.
In simple words, express is used to respond to the web requests sent by axios.
If you know about the fetch() method in javascript, axios is just an alternative to fetch().
I would say that express is used to create HTTP servers. So the server runs somewhere and responds to a request.
Axios is an HTTP client. It creates requests!
In very simple words axios is just passing the web request to the server-side (express). They basically work together (axios -> express -> DB)
Just reiterating/clarifying: (IF I understand correctly),
Also, it's my understanding that these should be two separate projects/folders so don't co-mingle the code with each other. (Not a 100% sure on that part)
© 2022 - 2024 — McMap. All rights reserved.