-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkcd-nodejs-proxy.js
36 lines (34 loc) · 969 Bytes
/
xkcd-nodejs-proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var fs = require('fs');
var http = require('http');
var https = require('https');
var url = require('url');
//var ROOT_DIR = ".";
http.createServer(function(req, res) {
var urlObj = url.parse(req.url, true, false);
//console.log("opening" + ROOT_DIR + urlObj.pathname);
//console.log(urlObj);
//if (urlObj.pathname == "/getxkcd") {
//console.log("In REST SERVICE")
https.get("https://xkcd.com/" + urlObj.query["q"] + "info.0.json", function(response) {
response.setEncoding('utf8')
response.on('data', function(d) {
try{
res.setHeader("Content-Type", "application/json");
res.writeHead(200);
res.end(d);
}
catch(e){}
});
//response.on('end', function() {
// res.end();
//});
})//.on('error', function(e) {
//console.log("Got error: " + e.message);
//res.write("error");
//res.end();
//});
// } else {
// res.writeHead(404);
// res.end("Not Found");
// }
}).listen(12894);