Skip to content

Instantly share code, notes, and snippets.

@magicly
Created November 12, 2015 11:46
Show Gist options
  • Save magicly/405cede475ad5130901d to your computer and use it in GitHub Desktop.
Save magicly/405cede475ad5130901d to your computer and use it in GitHub Desktop.
var http = require('http');
var url = require('url');
http.createServer(server).listen(1337, '127.0.0.1');
var qs = require('querystring');
var WHITE_LIST = new Set(["http://127.0.0.1:63342", "http://localhost:63342"]);
function server(request, response) {
var req = request;
var res = response;
console.log("origin: " + req.headers.origin);
//res.setHeader("Access-Control-Allow-Origin", '*');
if (WHITE_LIST.has(req.headers.origin)) {
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
}
//res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:63342");
if (request.method == 'OPTIONS') {
res.setHeader("Access-Control-Allow-Headers", "accept, content-type");
res.setHeader("Access-Control-Allow-Methods", "POST");
res.setHeader("Access-Control-Max-Age", 10);
res.setHeader("Access-Control-Allow-Credentials", true);
}
if (request.method == 'POST') {
var body = '';
var postData = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
postData = body;
console.log("end postData: " + postData);
res.setHeader("Access-Control-Allow-Credentials", true);
response.end("postData: " + postData);
});
} else {
var pathname = url.parse(req.url).pathname;
console.log(`req path: ${pathname}, req body: ${req.body}`);
res.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);
res.setHeader("Access-Control-Allow-Credentials", true);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("result: ");
res.end('Hello World\n');
console.log("ok...");
}
}
console.log("start server...");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment