Trouble in node.js sending data from html form to the server.

0

I have a question about AWS Cloud9 and nodejs.

I am using this nodejs tutorial to teach myself how to communicate between client side and server side:

https://dev.to/gbudjeakp/how-to-connect-your-client-side-to-your-server-side-using-node-and-express-2i71

I followed the tutorial and set it up on my laptop and everything worked fine. I even was able to figure out how to write the data sent from the client to a file using writeFileSync.

The problem is in trying to get it to work while on the Amazon Web Service Cloud9 integrated development environment.

When I run the index.js file by itself on Cloud9, I get the message stating that the server is running on port 8080, as expected.

However, I cannot get the HTML file to interface with the index.js file. The HTML file contains a form that is supposed to send the form content to index.js.

I believe the problem is in the address contained in the HTML form. I have researched and tried several different configurations, but none of them work. Currently, the form looks like this:

<form action="https://localhost:8080/login" method="POST"> <input type="text" name="username" placeholder="username"> <input type="text" name="password" placeholder="password"> <button type="submit">Login</button> </form>

Using localhost:8080 results in an error message of "localhost refused to connect."

When I have used variations of my username and AWS project name, I receive an error stating that the server IP address could not be found.

Any help would be appreciated.

asked 7 months ago57 views
1 Answer
1

Please refer to the documentation.

You are trying to go to an address that contains an IP of 127.0.0.1, localhost, or 0.0.0.0. The default, built-in behavior of the AWS Cloud9 IDE is that this will attempt to go to your local computer instead of attempting to go the instance or your own server that is connected to the environment.

So in your case, the HTML file you open in the Cloud9 IDE tries to connect to localhost which is then translated to your local machine, not the EC2 instance your Cloud9 environment (and your NodeJS server) runs on.

Instead, consider changing the target host in your HTML file.

  1. Start your NodeJS server in the terminal as described in your tutorial
  2. In Cloud9, go to Tools --> Preview --> Preview Running Application
  3. A new IDE tab will open, where you can see the output like "Welcome to your server"
  4. In that new preview tab, click on the address bar and copy the URL you'll find there, it will be something like https://12a34567b8cd9012345ef67abcd890e1.vfs.cloud9.us-west-1.amazonaws.com (with a different ID and probably a different region)
  5. Replace localhost in the HTML file with the host name from above
  6. Save your HTML file and try to test your app again

Alternatively, just make the action in your HTML form to a relative URL:

<form action="/login" method="POST">

Cloud9 will automatically forward the requests to your running application on ports 8080, 8081, or 8082. See the documentation for details.

profile picture
answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions