Difference between express.js and axios.js in Node
Asked Answered
O

5

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!

Overnight answered 7/6, 2020 at 10:37 Comment(3)
Have you read their respective website ?Directoire
express.js is web framework of javascript, but axios.js is Promise based HTTP client for the browser and node.js. they are do diffrent thingsWhom
I suggest you fo through this short description here - masteringjs.io/tutorials/express/routerTrophozoite
P
15

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

Posturize answered 7/6, 2020 at 14:25 Comment(2)
We use ajax requests for getting/posting an item. So is axios a way to send them securely?Overnight
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,...)Posturize
H
15

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

Heterogony answered 15/7, 2021 at 13:51 Comment(0)
O
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!

Ogburn answered 16/5, 2022 at 20:26 Comment(0)
P
0

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

Phenomenal answered 22/4, 2022 at 21:49 Comment(0)
S
0

Just reiterating/clarifying: (IF I understand correctly),

  • Use Axios to make API calls FROM your FRONT END UI, and
  • Use Express on your BACK-END SERVER to respond to API calls.
  • Express is connected to your database (Mongo, etc.)
  • Use Postman to send test requests to your back-end.
  • Use Mockable, Mockbin, Swagger, JSONPlaceholder, Amazon API gateway, or IBM to act as a server and respond to App requests with test data.

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)

Steapsin answered 28/5 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.