Created
August 11, 2021 19:41
-
-
Save matthewoestreich/2a376f40cced437d03ccc991c5cb51ee to your computer and use it in GitHub Desktop.
ssrPocNodeJS.js
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 https = require("https"); | |
const url = require("url"); | |
const server = http.createServer((request, response) => { | |
const path = url.parse(request.url).pathname; | |
switch (path) { | |
/** | |
* This is just documentation comments, doesn't do anything to the code | |
* @route '/' home path | |
* @method GET | |
*/ | |
case "/": | |
default: | |
https | |
.get("https://jsonplaceholder.typicode.com/todos/", (resp) => { | |
let data = ""; | |
// A chunk of data has been received. | |
resp.on("data", (chunk) => { | |
data += chunk; | |
}); | |
// The whole response has been received. Print out the result. and send | |
resp.on("end", () => { | |
console.log(JSON.parse(data).explanation); | |
response.writeHead(500); | |
response.write(`<h1>My App</h1><pre>${data}</pre>`); | |
}); | |
}) | |
.on("error", (err) => { | |
console.log("Error: " + err.message); | |
response.writeHead(500); | |
response.write(err.message); | |
}); | |
} | |
}); | |
server.listen(8008); | |
const serverAddress = server.address(); | |
const serverIp = serverAddress.address === "::" ? "localhost" : serverAddress.address; | |
console.log(`Server listening at : ${serverIp}:${serverAddress.port}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment