Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from mhoofman/master

Added JSONP Support
  • Loading branch information...
commit c748cc1639d60db7fb5ad4d2e556c86c646238f7 2 parents 79e4281 + fb98da5
@chriso authored
Showing with 16 additions and 8 deletions.
  1. +16 −8 server.js
View
24 server.js
@@ -1,4 +1,5 @@
var parsed = [], total = 0, inc, current;
+var fs = require('fs'), http = require('http'), url = require('url');
function csv(line) {
var matches, value, csv = []
@@ -16,7 +17,7 @@ function csv(line) {
return csv;
}
-inc = require('fs').readFileSync('inc.csv').toString().split('\r\n');
+inc = fs.readFileSync('inc.csv').toString().split('\r\n');
inc.shift(); //Header
@@ -41,12 +42,19 @@ inc.forEach(function (line) {
++total;
});
-require('http').createServer(function (request, response) {
-
- response.writeHead(200, {'content-type': 'application/json'});
- response.end(JSON.stringify(
- parsed[Math.round(total * Math.random())]
- ));
-
+http.createServer(function (request, response) {
+ var url_obj = url.parse(request.url, true);
+ var json_resp = JSON.stringify(
+ parsed[Math.round(total * Math.random())]);
+ var callback = url_obj.query["callback"];
+
+ if (callback) {
+ var jsonp = callback + "(" + json_resp + ");";
+ response.writeHead(200, {'content-type': 'application/javascript'});
+ response.end(jsonp);
+ } else {
+ response.writeHead(200, {'content-type': 'application/json'});
+ response.end(json_resp);
+ }
}).listen(process.env.PORT || 3000);
Please sign in to comment.
Something went wrong with that request. Please try again.