Skip to content

Instantly share code, notes, and snippets.

@megadix
Created November 8, 2019 17:00
Show Gist options
  • Select an option

  • Save megadix/c0a24d7235d2a12ade4038f65b4b2678 to your computer and use it in GitHub Desktop.

Select an option

Save megadix/c0a24d7235d2a12ade4038f65b4b2678 to your computer and use it in GitHub Desktop.
const axios = require('axios');
const restify = require('restify');
function respond(req, res, next) {
function ok() {
res.send('hello ' + req.params.name);
}
function error(code, data) {
res.send(code, data);
}
function timeout() {
setTimeout(ok, 3000);
}
// ok();
error(500, 'BOOOOM');
// timeout();
next();
}
const server = restify.createServer();
server.get('/hello/:name', respond);
server.head('/hello/:name', respond);
server.listen(3000, function() {
console.log('%s listening at %s', server.name, server.url);
});
const httpClient = axios.create({
baseURL: 'http://localhost:3000',
timeout: 1000
});
httpClient.get('/hello/Dimitri')
.then(res => {
console.log(res.data);
})
.catch(error => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(`HTTP Error: ${error.response.status}`);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of http.ClientRequest
console.log(error.message);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Axios error e.g. invalid URL or params');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment