Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between express.js and axios.js in Node

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!

like image 566
TANMAY AGRAWAL Avatar asked Jan 25 '23 23:01

TANMAY AGRAWAL


2 Answers

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')

like image 69
Xuan Son NGUYEN Avatar answered Jan 28 '23 23:01

Xuan Son NGUYEN


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().

like image 32
Sudhanshu Bhardwaj Avatar answered Jan 28 '23 23:01

Sudhanshu Bhardwaj