Skip to content

Instantly share code, notes, and snippets.

@robertcoopercode
Created November 12, 2017 19:51
Show Gist options
  • Save robertcoopercode/8493e84f00b1d0049542e38e03e9ea65 to your computer and use it in GitHub Desktop.
Save robertcoopercode/8493e84f00b1d0049542e38e03e9ea65 to your computer and use it in GitHub Desktop.
// Route that generates the lorem ipsum text and reloads a modified index.html
router.post('/', (request, response) => {
request.on("data", function(inputValue) {
// Convert the POST data into a readable string
let query = inputValue.toString(); // i.e. numberOfParagraphs=3
// Parse the query into a key/value pair and store the value of numberOfParagraphs
// in a variable
let numberOfParagraphs = querystring.parse(query).numberOfParagraphs;
// Generate the lorem ipsum text with the getAllParagraphs function
let loremIpsumText = loremIpsum.getAllParagraphs(numberOfParagraphs);
// Capture the contents of index.html in a variable
let fileContents = fs.readFileSync("./public/index.html", {encoding: "utf8"});
// Replace the placeholder <div> with the lorem ipsum text
fileContents = fileContents.replace("<div class='placeholder-div'></div>",loremIpsumText);;
response.setHeader('Content-Type', 'text/html');
// Send a response to the client with the modified index.html file
response.write(fileContents);
response.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment