Skip to content

Instantly share code, notes, and snippets.

@simon-saliba
Created April 10, 2021 16:31
Show Gist options
  • Save simon-saliba/efdd64399d2c49c3e4d197ca069b6885 to your computer and use it in GitHub Desktop.
Save simon-saliba/efdd64399d2c49c3e4d197ca069b6885 to your computer and use it in GitHub Desktop.
Github Repo Number Retriever Without Redis
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