0

To elaborate on the question in the title,

I have made a simple app with js that runs on a node server. I have a thumbdrive that contains a folder and a start.bat file. Start.bat, as the name implies, switches the directory to my server folder and starts the server. Start.bat also starts another process that opens the edge browser to localhost in kiosk mode. When a user starts start.bat, the app will appear on the screen with the server running in the background. When the user exits the edge browser, they are then required to CTRL + C out of the server cmd prompt to properly shut down the server.

I need a system which effectively automatically shuts down the server after the Edge browser has been closed. I am not sure if it is possible to tie together the closing of the browser and the node server and am yet to find a solution online. If anyone has any ideas regarding possible fixes to my problem I would love to hear it!

https-server.js

const https = require("https");
const path = require("path");
const fs = require("fs");
const ip = require("ip");
const process = require("process");

const app = express();
const port = 443;

process.chdir("..");
console.log("Current working dir: " + process.cwd());
var rootDir = process.cwd();

//determines what folder houses js, css, html, etc files
app.use(express.static(rootDir + "/public/"), function (req, res, next) {
  const ip = req.ip;

  console.log("Now serving ip:", "\x1b[33m", ip, "\x1b[37m");
  next();
});

//determines which file is the index
app.get("/", function (req, res) {
  res.sendFile(path.join(rootDir + "/public/index.html"));
});

var sslServer = https.createServer(
  {
    key: fs.readFileSync(path.join(rootDir, "certificate", "key.pem")),
    cert: fs.readFileSync(path.join(rootDir, "certificate", "certificate.pem")),
  },
  app
);

//determines which port app (http server) should listen on
sslServer.listen(port, function () {
  console.log(
    "Server has successfully started, available on:",
    "\x1b[33m",
    ip.address(),
    "\x1b[37m",
    "listening on port:",
    "\x1b[33m",
    +port,
    "\x1b[37m"
  );
  console.log("CTRL + C to exit server");
  sslServer.close();
});

Will provide any needed information.

david b
  • 51
  • 1
  • 6
  • You can make a request to the server to shut it down. Refer to the [`window.onbeforeonload` event]( https://stackoverflow.com/questions/13443503/run-javascript-code-on-window-close-or-page-refresh ) – Oluwafemi Sule May 26 '21 at 20:50
  • @Oluwafemi Sule Yes, I have a windows.onbeforeonload event on my index.html script file. All it currently does is console.log("Browser closed"). Could you elaborate on how I could use this to request that the server closes? – david b May 27 '21 at 04:35

1 Answers1

0

Have an endpoint registered to exit the process

app.get('/shutdown', (req, res, next) => {
   res.json({"message": "Received"});
   next();
}, () => {
  process.exit();
});

Then register a listener for onbeforeunload to do a request to this endpoint.

  let terminateCmdReceived = false;
  async function shutdown(e) {
    let response;
    if (!terminateCmdReceived) {
      e.preventDefault();
      try {
        response = await fetch('http://localhost:3000/shutdown');
        const json = await response.json();
        if(json.message === "Received") {
          terminateCmdReceived = true; 
          window.close();
        }
      } catch (e) {
        console.error("Terminate Command was not received");    
      }
    }
    
  }
  window.onbeforeunload = shutdown
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81