From 25797283846be20daa45c07c4b1cf4b5d70d114c Mon Sep 17 00:00:00 2001 From: Matt Zabriskie Date: Thu, 28 Aug 2014 15:57:35 -0600 Subject: [PATCH] Adding sandbox --- sandbox/index.html | 164 +++++++++++++++++++++++++++++++++++++++++++++ sandbox/index.js | 63 +++++++++++++++++ 2 files changed, 227 insertions(+) create mode 100644 sandbox/index.html create mode 100644 sandbox/index.js diff --git a/sandbox/index.html b/sandbox/index.html new file mode 100644 index 0000000000..c7d7688221 --- /dev/null +++ b/sandbox/index.html @@ -0,0 +1,164 @@ + + + + axios + + + + +

axios

+ +
+

Input

+
+
+ + +
+
+ + +
+ +
+ + +
+ +
+
+ +
+

Request

+
No Data
+
+ +
+

Response

+
No Data
+
+ + + + + \ No newline at end of file diff --git a/sandbox/index.js b/sandbox/index.js new file mode 100644 index 0000000000..b42ac8e9fd --- /dev/null +++ b/sandbox/index.js @@ -0,0 +1,63 @@ +var fs = require('fs'); +var url = require('url'); +var path = require('path'); +var http = require('http'); +var server; + +server = http.createServer(function (req, res) { + req.setEncoding('utf8'); + + var parsed = url.parse(req.url, true); + var pathname = parsed.pathname; + + console.log('[' + new Date() + ']', req.method, pathname); + + if (pathname === '/') { + pathname = '/index.html'; + } + + if (pathname === '/index.html') { + fs.createReadStream(path.join(__dirname, pathname)).pipe(res); + } else if (pathname === '/axios.js') { + res.writeHead(200, { + 'Content-Type': 'text/javascript' + }); + fs.createReadStream(path.join(__dirname, '../dist/axios.js')).pipe(res); + } else if (pathname === '/api') { + var status; + var result; + var data = ''; + + req.on('data', function (chunk) { + data += chunk; + }); + + req.on('end', function () { + try { + status = 200; + result = { + url: req.url, + data: data ? JSON.parse(data) : undefined, + method: req.method, + headers: req.headers + }; + } catch (e) { + console.error('Error:', e.message); + status = 400; + result = { + error: e.message + }; + } + + res.writeHead(status, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify(result)); + }); + } else { + res.writeHead(404); + res.end('

404 Not Found

'); + } +}); + +server.listen(3000); \ No newline at end of file