Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Created August 11, 2021 19:41
Show Gist options
  • Save matthewoestreich/2a376f40cced437d03ccc991c5cb51ee to your computer and use it in GitHub Desktop.
Save matthewoestreich/2a376f40cced437d03ccc991c5cb51ee to your computer and use it in GitHub Desktop.
ssrPocNodeJS.js
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