0

I'm experimenting with the sample Node.js app provided by AWS Elastic Beanstalk, and I want to dynamically insert content into the index.html file from the app.js file.

I am just trying to get familiar with the platform and run some very basic tests. I have looked extensively for answers, the closet thing I've found is here: How to change front-end html using node.js but I do not understand the answer and I'm not sure it's relevant to me. I am just working off the literal sample code provided by AWS so I was hoping this would be simple but it has been throwing me for a loop.

var html = fs.readFileSync('index.html');

is defined at the beginning of the file, and at the end of the function http.createServer(function (req, res) is the following lines:

res.writeHead(200);
res.write(html);
res.end();

From my understanding, this is how the index gets loaded and rendered to the user, upon a GET request. I have tried replaced the 2nd line with res.write("Hello"); and res.write("<p>Hello</p>");, thinking that my entire page would get replaced with a simple Hello but there was no change whatsoever.

I would just like to know how to edit the index.html file from this app.js file. Anything to be able to make a change would be very helpful. Please advise. Let me know if more info is needed. Thank you in advance.

4
  • I think the answer in that link is relevant to you. The issue is your mental model of Javascript execution. There are two places where Javascript may be executed: on the server (in Node.js environment) and on the client (in the browser, presumably Chrome or something). Can you be more specific about the type of change you're trying to make to your HTML? Are you just trying to populate elements, or are you trying to do something more complex like replacing an element? Commented Aug 21, 2019 at 22:12
  • ALso: can you link to the AWS example you're referencing? I would like to read it to understand the context of the example they're giving. Commented Aug 21, 2019 at 22:16
  • The sample application can be found here: docs.aws.amazon.com/elasticbeanstalk/latest/dg/…, I'm working off the node.js version. Commented Aug 22, 2019 at 13:06
  • Weirdest thing is, when I came in to work this morning, my application actually just said "hello" like I expected. I guess it needed some time for the changes to take effect. But to answer your question, I was just trying to create & populate elements, not replace any existing HTML. Commented Aug 22, 2019 at 13:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.