Last active
September 19, 2020 14:36
-
-
Save cscalfani/e7fdc5ad46486142b6d8772ad047fa8f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const http = require("http"); | |
const hostname = "0.0.0.0"; | |
const port = 3000; | |
const server = http.createServer((req, res) => { | |
console.log(`\n${req.method} ${req.url}`); | |
console.log(req.headers); | |
const reverseArray = a => { | |
var na = []; | |
for (var i = 0; i < a.length; ++i) { | |
const value = a[i]; | |
const nValue = value.constructor.name == 'Object' | |
? reverseKeys(value) : value; | |
na.push(nValue); | |
} | |
return na; | |
} | |
const reverseKeys = o => { | |
const no = {}; | |
for (const [key, value] of Object.entries(o)) { | |
const nValue = value.constructor.name == 'Object' | |
? reverseKeys(value) | |
: (value.constructor.name == 'Array' | |
? reverseArray(value) : value); | |
no[key.split("").reverse().join("")] = nValue; | |
} | |
return no; | |
}; | |
req.on("data", function (chunk) { | |
console.log("BODY: " + chunk); | |
res.statusCode = 200; | |
res.setHeader("Content-Type", "text/plain"); | |
const o = reverseKeys(JSON.parse(chunk)); | |
const response = JSON.stringify(o, null, 2); | |
console.log("RESPONSE: " + response); | |
res.end(response); | |
}); | |
}); | |
server.listen(port, hostname, () => { | |
console.log(`Server running at http://localhost:${port}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment