Last active
October 31, 2018 20:50
-
-
Save NickNaso/8355a4601fe2543e35a2888e5ef6c350 to your computer and use it in GitHub Desktop.
Example how to use axios from Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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