Created
September 18, 2017 14:31
-
-
Save skippednote/365fdb5f5bbf96930c19fd0e1bade0e9 to your computer and use it in GitHub Desktop.
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
require('isomorphic-fetch') | |
const api = (query) => { | |
return fetch(`https://reddit.com/r/${query}.json`) | |
.then(res => res.json()) | |
.then(data => { | |
return data.data.children.reduce((acc, i) => { | |
acc += `<li>${i.data.title}</li>` | |
return acc; | |
}, `<ul>`) | |
}) | |
.catch(err => console.error(err)) | |
} | |
module.exports = api; |
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 api = require('./api') | |
const app = express() | |
app.use(express.static('public')) | |
const contentHeader = ` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Streaming Content</title> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
` | |
const contentFooter = ` | |
</body> | |
</html> | |
` | |
app.get('/', async (req, res) => { | |
res.write(contentHeader) | |
res.flushHeaders() | |
const body = await api('javascript') | |
res.write(body) | |
res.flushHeaders() | |
res.write(contentFooter) | |
res.end() | |
}) | |
app.listen(8080, () => { | |
console.log('Listening on port 8080') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment