title: How to run and stop a subserver node.js with spawn?
body: <p>I would like to start/stop a second app with node.js</p>
  
  <p>I have the following 2 nodejs applications in a directory:</p>
  
  <pre><code>--app.js
   |
   -app2.js
  </code></pre>
  
  <p>Inside app.js</p>
  
  <pre><code>serverProcess = spawn('node', ['app2.js']);
  process.stdin.pipe(serverProcess.stdin);
  
  serverProcess.stdout.on('data', data =&gt; {
      console.log(`child stdout:\n${data}`);
  });
  
  setTimeout(function() {
      console.log('kill');
      serverProcess.stdin.pause();
      serverProcess.kill();
  }, 5000);
  </code></pre>
  
  <p>Inside app2.js</p>
  
  <pre><code>const express = require('express')
  const app = express()
  const port = 3000
  
  app.get('/', (req, res) =&gt; res.send('Hello World!'))
  
  app.listen(port, () =&gt; console.log(`Example app listening on port ${port}!`))
  </code></pre>
  
  <p>What I would like to do is run app.js which runs app2.js for 5 seconds, logs the output to stdout, so I can see it, then terminate the process.</p>
  
  <p>Currently, <code>app2.js</code> is killed, but <code>app.js</code> continues running and it is not terminating.</p>
  
  <p>How would I correct my code so that <code>app.js</code> terminates as soon as <code>app2.js</code> is killed?</p>