Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Last active October 31, 2018 20:50
Show Gist options
  • Save NickNaso/8355a4601fe2543e35a2888e5ef6c350 to your computer and use it in GitHub Desktop.
Save NickNaso/8355a4601fe2543e35a2888e5ef6c350 to your computer and use it in GitHub Desktop.
Example how to use axios from Node.js
'use strict'
const axios = require('axios')
const express = require('express')
const app = express()
app.use((err, req, res, next) => {
console.error('Error happened ...')
console.log(err)
res.status(500).json({error: 'Error happened on your application', code: 'E500'})
})
app.get('/', async (req, res, next) => {
try {
const [ userResponse, reposResponse ] = await Promise.all([
axios.get('https://api.github.com/users/NickNaso'),
axios.get('https://api.github.com/users/NickNaso/repos')
])
console.log(userResponse.data)
console.log(reposResponse.data)
res.status(200).json({user: userResponse.data, repos: reposResponse.data})
} catch(err) {
next(err)
}
})
app.listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment