Created
October 31, 2019 11:46
-
-
Save omar2205/1f2178b57ff7b9edd857d17e1af3db53 to your computer and use it in GitHub Desktop.
express async fetch
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
const app = require('express')() | |
const fetch = require('node-fetch') | |
const API_KEY = process.env.API_KEY | |
app.get('/posts', async (req, res) => { | |
const getPosts = () => fetch(`/api/key=${API_KEY}`) | |
const processPosts = async () => { | |
const posts = await getPosts() | |
const postsJSON = await posts.json() | |
res.end(JSON.stringify(postsJSON) | |
} | |
processPosts() | |
}) | |
app.listen(process.env.PORT || 8080, () => console.log('server is online')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment