Skip to content

Instantly share code, notes, and snippets.

@Jeija
Created July 7, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeija/c4af7a077882c92c160bd8280bb6bd4d to your computer and use it in GitHub Desktop.
Save Jeija/c4af7a077882c92c160bd8280bb6bd4d to your computer and use it in GitHub Desktop.
Sponsorenlauf
$.post("http://localhost:1234/action/register_runner", JSON.stringify({name : "Kjell"}), function(response) { console.log(response); });
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