Created
July 7, 2017 14:34
-
-
Save Jeija/c4af7a077882c92c160bd8280bb6bd4d to your computer and use it in GitHub Desktop.
Sponsorenlauf
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
$.post("http://localhost:1234/action/register_runner", JSON.stringify({name : "Kjell"}), function(response) { console.log(response); }); |
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 TCPPORT = 1234; | |
var http = require("http"); | |
function register_runner(name, data, res) { | |
console.log(data.name); | |
res.write("ok\n"); | |
res.end(); | |
} | |
function startCommand(name, data, res) { | |
if (name == "register_runner") { | |
register_runner(name, data, res); | |
} | |
} | |
function parseCommand(name, req, res) { | |
var POST = ""; | |
req.on("data", function(data) { POST += data; }); | |
req.on("end", function() { | |
var post_data; | |
try { | |
post_data = JSON.parse(POST); | |
startCommand(name, post_data, res); | |
} catch(e) { | |
console.log("Could not parse HTTP command: " + e); | |
res.end("error: " + e); | |
return; | |
} | |
}); | |
} | |
http.createServer(function (req, res) { | |
// Parse HTTP query | |
var fragments = req.url.substring(1).split("/"); | |
var query = fragments.splice(0, 2); | |
query.push(fragments.join("/")); | |
if (query[0] == "ping") { | |
res.writeHead(204, {"Access-Control-Allow-Origin" : "*"}); | |
res.end(); | |
} else if (query[0] == "action") { | |
res.writeHead(200, {"Content-Type": "text/plain", | |
"Access-Control-Allow-Origin" : "*"}); | |
if (req.method == "POST") { | |
parseCommand(query[1], req, res); | |
} else { | |
// Propably a CORS preflight | |
res.end(); | |
} | |
} else { | |
// empty answer, but with Access-Control-Allow-Origin: * | |
res.writeHead(200, {"Content-Type": "text/plain", | |
"Access-Control-Allow-Origin" : "*"}); | |
res.end(); | |
} | |
}).listen(TCPPORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment