Created
March 24, 2016 02:14
-
-
Save ahdinosaur/42c583e85ba1ee642b1d to your computer and use it in GitHub Desktop.
live http server / client
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
{ | |
"animal": "chinchilla" | |
} |
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
var fs = require('fs') | |
var express = require('express') | |
var clientGet = require('simple-get') | |
var dataServer = express() | |
dataServer.get('/data', function (req, res) { | |
readJson('./data.json', function (err, data) { | |
if (err) { | |
return res.status(500).send({ error: err }) | |
} | |
res.send(data) | |
}) | |
}) | |
dataServer.listen(5000, function () { | |
console.log('data server listening on http://localhost:5000') | |
}) | |
var loudServer = express() | |
loudServer.get('/loud', function (req, res) { | |
getJson('http://localhost:5000/data', function (err, data) { | |
if (err) { | |
return res.status(500).send({ error: err }) | |
} | |
res.send(loudify(data)) | |
}) | |
}) | |
loudServer.listen(5001, function () { | |
console.log('loud server listening on http://localhost:5001') | |
}) | |
var slowServer = express() | |
slowServer.get('/slow', function (req, res) { | |
setTimeout(function () { | |
clientGet.concat('http://localhost:5001/loud', function (err, response, data) { | |
if (err) { | |
return res.status(500).send({ error: err }) | |
} | |
res.setHeader('content-type', 'application/json') | |
res.send(data) | |
}) | |
}, 2 * 1000) // 2 * 1000 milliseconds = 2 seconds | |
}) | |
slowServer.listen(5002, function () { | |
console.log('slow server listening on http://localhost:5002') | |
}) | |
// utility functions | |
function loudify (object) { | |
var returnObject = {} | |
var keys = Object.keys(object) | |
keys.forEach(function (key) { | |
var value = object[key] | |
returnObject[key.toUpperCase()] = value.toUpperCase() | |
}) | |
return returnObject | |
} | |
function getJson (url, callback) { | |
clientGet.concat(url, function (err, res, data) { | |
console.log('data', data.toString()) | |
if (err) { callback(err) } | |
else { callback(null, JSON.parse(data)) } | |
}) | |
} | |
function readJson (path, callback) { | |
fs.readFile(path, 'utf8', function (err, data) { | |
if (err) { callback(err) } | |
else { callback(null, JSON.parse(data)) } | |
}) | |
} | |
/* | |
TODO diagram slow server | |
--- | |
browser | |
client <--------- | |
|| | responds to browser with loudify(data) | |
|v | | |
||----> loud server listens to GET /loud | |
|| ^ | | |
|| | | loud server is client to data server | |
|| | | makes GET /data request | |
|| | v | |
||----> data server listens to GET /data | |
------< responds with json in data.json | |
*/ | |
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
{ | |
"name": "http-ping-pong", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "Mikey <[email protected]> (http://dinosaur.os)", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.15.0", | |
"express": "^4.13.4", | |
"simple-get": "^2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment