Last active
August 29, 2015 14:10
-
-
Save oscarryz/92c6bf5582e6dc8a8272 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
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