I have a js file batch.js
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.
Asked
Active
Viewed 126 times
1

Jean Dupont
- 177
- 1
- 13
-
2This might cover the part where you launch your batch in NodeJS : [Start another node application using node.js?](http://stackoverflow.com/questions/18862214/start-another-node-application-using-node-js) – blex Jun 14 '16 at 21:45
-
1Then you'll need to send an AJAX request from your front-end to your server. On the server side, do what @blex points to. – Mike Cluck Jun 14 '16 at 21:47
-
@MikeC Hii Mike, I didn't get it well, please can you guide me ? – Jean Dupont Jun 15 '16 at 11:23
1 Answers
2
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
.

Quentin
- 914,110
- 126
- 1,211
- 1,335
-
Thank you, I didn't get it well, can you give me a a brief example ? – Jean Dupont Jun 14 '16 at 22:14
-
A brief example of refactoring code I haven't seen into a module, writing a web server in JavaScript, and writing an HTML form? No. That isn't something that can really be covered briefly. – Quentin Jun 14 '16 at 22:19