repl.it
Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const https = require('https');
var http = require('http');
var urllib = require('urllib');
var url = require('url');

const btoa = function(str){ return Buffer.from(str).toString('base64'); }
const atob = function(str){
	return Buffer.from(b64Encoded, str).toString()
}
function ressend(res, code, ctype, chunk) {
	res.writeHead(code, {
		"Content-Type": ctype
	})
	res.write(chunk)
	res.end()
}
http.createServer(function(req, res) {
	if(req.method === "POST") {
		var data = ""
		var bad = false
		req.on("data", function(chunk) {
			data += chunk
			// Message shouldn't be longer than 1MB
			if(data.length > 1e6) {
				data = ""
				//req.connection.destroy()
				//bad = true
				ressend(res, 413, "text/plain", "413: Request entity too large")
			}
		})
		req.on("end", function() {
			console.log("POST RECIEVED: " + JSON.stringify(data))
		})
	} else if(req.method === "GET") {
		var url_parts = url.parse(req.url, true);
		var query = url_parts.query;
		console.log("GET RECIEVED: " + JSON.stringify(url_parts))

		https.get('https://kahoot.it/reserve/session'+url_parts.pathname, (resp) => {
			// A chunk of data has been recieved.
			resp.on('data', (chunk) => {
				console.log("- - - - - - Session Data - - - - - -\n"+chunk)
				ressend(res, 200, "text/html", (chunk))
			});
			// The whole response has been received. Print out the result.
			resp.on('end', () => {
				//console.log(JSON.parse(data).explanation);
			});
		}).on("error", (err) => {
			console.log("Error: " + err.message);
			ressend(res, 400, "text/html", "400: Bad Request"+err.message)
		});
		// Send the GUI
	}
}).listen(3000)
?