Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarryz/92c6bf5582e6dc8a8272 to your computer and use it in GitHub Desktop.
Save oscarryz/92c6bf5582e6dc8a8272 to your computer and use it in GitHub Desktop.
var http = require('http')
var server = http.createServer(function(req,res){
console.log( req.url )
var helloRE = new RegExp("^/hello/(\\d)$")
if ( helloRE.test(req.url) ) {
var groups = helloRE.exec(req.url)
var id = groups[1]
var options = {
hostname: 'jsonplaceholder.typicode.com',
port: 80,
path: '/users/'+id,
method: 'GET'
};
var cr = http.request(options,function(r) {
r.setEncoding('utf8');
r.on('data', function(chunk) {
var json = JSON.parse(chunk)
res.writeHead( 200 )
res.write("The name for id "+id+" is "+json.name)
res.end("")
})
})
cr.end()
} else {
res.writeHead( 404 )
res.end("Usage: /hello/id where id = single digit")
}
}).listen(1337, 'localhost')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment