0

I am working on a node.js project where I have to define different endpoints to open and close browsers on specific endpoints.

Like on: localhost:3000/open---> opens the browser localhost:3000/close---> closes the browser

I have made child process that opens the browser and kills it but how to integrate it with a express api,Any idea?

Here's my code:

var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
//open browser
var url = 'https://www.google.com';
var cmd = start + ' ' + url;
var child = require('child_process').exec(cmd, function(error, stdout, stderr) {
    if (error) {
        console.log('exec error: ' + error);
    }
});
  
//close browser
process.on('SIGINT', function() {
    child.kill();
    process.exit();
});
rudeTool
  • 526
  • 1
  • 11
  • 25
  • `Any idea?` - yeah, write the express code and integrate your code - of course, you can't open the browser from express because the browser would need to be open to send the request to express, but since the browser is open, you don't need to open the browser ... which, then of course means you can't close the browser, since your code would not know what to close, because it didn't open it ... so, I was wrong, you can't possibly achieve it – Bravo Aug 06 '21 at 06:18
  • I told you, they way you write it its not possible... If you would start the browser without "open" it would be possible. – Marc Aug 06 '21 at 08:10
  • Does this answer your question? [not able to kill process in node.js](https://stackoverflow.com/questions/68677492/not-able-to-kill-process-in-node-js) – Marc Aug 06 '21 at 08:12
  • I figured out a way using spawn method – rudeTool Aug 06 '21 at 08:16

0 Answers0