Jean Dupont Jean Dupont - 1 year ago 75
Node.js Question

Launch or compile a Js file via a button in my frontend page html

I have a js file

which do a batch insert for me, but I must launch it from my terminal :
node batch.js
. Now, I want to create a button on my frontend html page, and launch my file via the button.

Answer Source

You can't, per se.

You can use the browser to request a URL (and you can do it from a button by submitting a form), but you need an HTTP server listen to the request for the URL and react accordingly.

Ideally you would rewrite batch.js so it was a module and then write a webserver in Node (e.g. using Express) which would load the module and call a function exported from it when the given URL was requested (and then respond to the browser).

More hackily, you would spawn a new instance of node whenever that URL was requested and run your batch.js without modification. That would let you use any HTTP server you like and any programming language to write the server side program that would execute node batch.js.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download