execute shell command from html button node js
- Create a new Node.js project and navigate into the project directory.
- Install the
express
and child_process
modules using npm. - Create a new file (e.g.,
app.js
) and require the necessary modules at the beginning. - Initialize an Express app and set up a route to handle the button click from the HTML page.
- Define a function to execute the shell command using the
child_process
module's exec
function when the button is clicked. - In the function, use the
exec
function to run the desired shell command, such as ls
or echo "Hello"
. - Use
res.send
to send a response back to the HTML page, indicating the command has been executed. - Start the Express app and listen on a specific port.