Created
April 10, 2021 16:31
-
-
Save simon-saliba/efdd64399d2c49c3e4d197ca069b6885 to your computer and use it in GitHub Desktop.
Github Repo Number Retriever Without Redis
This file contains 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
const express = require('express'); | |
const fetch = require('node-fetch'); | |
const PORT = process.env.PORT || 5000; | |
const app = express(); | |
// Set response | |
function setResponse(username, repos) { | |
return `<h2>${username} has ${repos} Github repos</h2>`; | |
} | |
// Make request to Github for data | |
async function getRepos(req, res, next) { | |
try { | |
console.log('Fetching Data...'); | |
const { username } = req.params; | |
const response = await fetch(`https://api.github.com/users/${username}`); | |
const data = await response.json(); | |
const repos = data.public_repos; | |
res.send(setResponse(username, repos)); | |
} catch (err) { | |
console.error(err); | |
res.status(500); | |
} | |
} | |
app.get('/repos/:username', getRepos); | |
app.listen(PORT, () => { | |
console.log(`App listening on port ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment