Created
November 12, 2015 11:46
-
-
Save magicly/405cede475ad5130901d to your computer and use it in GitHub Desktop.
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 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