-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (32 loc) · 1.26 KB
/
server.js
File metadata and controls
38 lines (32 loc) · 1.26 KB
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
37
38
// server.js
var fs = require('fs'),
url = require('url'),
http = require('http'),
port = process.env.PORT || 3000,
usage = fs.readFileSync('usage.html', 'utf8');
http.createServer(function (req, res) {
'use strict';
console.log('Path: ' + url.parse(req.url).path);
if (url.parse(req.url).pathname.toLowerCase() != '/jsonp') {
res.writeHead(404, {'Content-Type': 'text/html'});
res.write(usage, 'utf8');
res.end();
} else {
res.writeHead(200, {'Content-Type': 'application/javascript'});
var options = url.parse(decodeURI(url.parse(req.url, true).query.url)); // parse url query string
res.write(decodeURI(url.parse(req.url, true).query.callback) + '(' + JSON.stringify(options) + ','); // callback
http.get(options, function (getres) { //{host: options.host, path: options.path}
res.write('"' + getres.statusCode + '",' + JSON.stringify(getres.headers) + ',"');
getres.setEncoding('utf8');
getres.on('data', function (chunk) {
res.write(encodeURI(chunk));
});
getres.on('end', function () {
res.end('");');
});
}).on('error', function (e) {
console.log("Error in http.get: " + e.message);
});
}
}).listen(port);
console.log('Server running on port ' + port + '.');