Skip to content

Instantly share code, notes, and snippets.

@mreis1
Created April 17, 2020 15:36
Show Gist options
  • Select an option

  • Save mreis1/e59ca0cca7c4d440011f8fbc69f4f5d1 to your computer and use it in GitHub Desktop.

Select an option

Save mreis1/e59ca0cca7c4d440011f8fbc69f4f5d1 to your computer and use it in GitHub Desktop.
node-axios-test (0.19.2)

A sample app that allow to quickly test how axios behave to different status codes and how to obtain the response data (if available).

How to test: start the client.js start the server.js

Then open in your browser: localhost:3334/404

404 is the status code that the server.js that will be used by the server to reply to you HTTP request.

const app = require('express')();
var axios = require('axios').default;
app.get('/:id', function (req, res) {
axios.get('http://localhost:3333/' + req.params.id)
.then((data) => {
console.log('Ok: ' + data);
res.json(data.data)
})
.catch((err) => {
/**
* Axios will throw if status code differs from 2XX.
*/
console.log('Error: ', err.response.data);
res.json([
err.response.status,
err.response.statusText,
err.response.headers,
err.response.data
])
});
});
app.listen(3334);
{
"name": "axios-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"express": "^4.17.1"
}
}
const app = require('express')();
app.get('/:id', function (req, res) {
res.status(req.params.id).json({some: 'data'});
});
app.listen(3333);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment