Home > Blockchain >  How do I pass a data from an HTML form to my Node.js file?
How do I pass a data from an HTML form to my Node.js file?

Time:02-17

Hope you are well. I am currently new to this side of web-development, and was wondering how my HTML files communicate with the Node.js side of things.

So from my understanding, Node.js only runs from the command line through "node file.js" and my HTML files run on my browser, but how do they communicate?

I have a few promises on my Node.js file which get data from an API endpoint, but currently to test, I define the "ID" (which i send to the endpoint) in the Node.js manually, how do I let the HTML (user) do this? and vice versa, how do I pass the JSON data which i receive to the HTML side.

Im extremely sorry if i've asked the wrong question, quite new to this type of development. Thank you so much in advance :)

CodePudding user response:

So from my understanding, Node.js only runs from the command line through "node file.js" and my HTML files run on my browser, but how do they communicate?

Most commonly, when you want Node.js and an HTML document to communicate you would write an HTTP server using Node.js (often with the Express.js framework) and then make HTTP requests (e.g. with a form submission or Ajax).

There are other options, such as Electron or OpenFin which are used for building desktop apps wrapped around HTML and Node.

CodePudding user response:

In Node.js you have the option to build a server with Express, you can look at the documentation and see how it works. in express, you will send REST API requests. you can send HTML files in the request to communicate with the client.

But if you want to serve static files such as images, CSS files, and JavaScript files, use the express static built-in middleware function in Express.

  • Related