How do I use axios within ExpressJS?
You don't have to enclose your axios call in a
try...catch
as axios already has a catch block.Your express handler has to send a response back when axios gets a response from the API call or axios catches an error during the API call.
Your code should look something like this
router.get("/test", (req, res, next) => { console.log("'/test' call"); axios.get("https://api.neoscan.io/api/main_net/v1/get_all_nodes") .then(data => res.json(data)) .catch(err => next(err));})
If you fancy async...await
, you can write your code like this
router.get("/test", async (req, res, next) => { console.log("'/test' call"); try { const res = await axios.get("https://api.neoscan.io/api/main_net/v1/get_all_nodes"); res.json(data); } catch (err) { next(err) }})