1

I have a script Start.js which is starting another script Server.js using shelljs.

Server.js contains a express server which should run at process.env.PORT.

Start.js should print out if the server was started correctly or not.

const process = shell.exec(`node Server.js`, {
            async: true,
            env: {
                PORT: 3000
            }
        }) as ChildProcess;

// how to detect that server was successfully started?

How can this script detect if the server was successfully started?

kpalatzky
  • 1,213
  • 1
  • 11
  • 26

1 Answers1

0

You'll need to write to stdout in Server.js, and listen to stdout in the exec callback function.

// Shell.js docs
exec('some_long_running_process', function(code, stdout, stderr) {
  console.log('Exit code:', code);
  console.log('Program output:', stdout);
  console.log('Program stderr:', stderr);
});

// Example
exec('node Server.js', function(code, stdout, stderr) {
  console.log('Exit code:', code);
  console.log('Program output:', stdout); // could print: 'server up!'
  console.log('Program stderr:', stderr);
});
CoderXYZ
  • 167
  • 2
  • 13