11

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!

10may
  • 311
  • 1
  • 2
  • 10

4 Answers4

13

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

Xuan Son NGUYEN
  • 366
  • 2
  • 5
  • We use ajax requests for getting/posting an item. So is axios a way to send them securely? – 10may Jun 08 '20 at 05:49
  • 3
    ajax and axios do exactly the same thing. axios isn't more secure, but personally I think it's easier to use (less code to write, promise-based,...) – Xuan Son NGUYEN Jun 08 '20 at 06:21
13

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

2

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!

sumsumcity
  • 217
  • 4
  • 3
0

In very simple words axios is just passing the web request to the server-side (express). They basically work together (axios -> express -> DB)