title: Node.js http request close response after eventEmitter callback
body: <p>I have a node.js ES6 project running on a Raspberry Pi with a main file and some imported ES6 child modules (http and a temperature sensor in the example).</p>
  
  <p>When the server receives a request I would want to call an async parent function (I can do this with EventEmitter) and on callback print temperature value on the page. </p>
  
  <p>Am I allowed to do this? 
  The problem is that response is not closed (so client page never finish) and if I repeat the call it tells me that the server :port is already busy.</p>
  
  <p><strong>Short summary</strong></p>
  
  <pre><code>...
  self.webserver.on('request', async (req, res) =&gt; {
    ...
    self.emit('remoteCall', path, JSON.stringify(queryData)); // call parent
    self.on('callback',(data)=&gt;{ // &lt;-- HERE callback from index.js with temperature data collected
     res.write(data);
     res.end(); // &lt;-- I want to wait for temperature data and then end server response
    });
  });
  ...
  </code></pre>
  
  <p><strong>Full example</strong></p>
  
  <pre><code>/**
   * ./app/services/server/server.js Server module
   */
  const http = require('http');
  const url = require('url');
  const EventEmitter = require( 'events' );
  
  class Server extends EventEmitter {
    constructor(){
      super();
      this.webserver = http.createServer();
      this.remoteServer = 'https://example.com/';
    }
    startServer(){
      const self = this;
      self.webserver.on('request', async (req, res) =&gt; {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        const q = url.parse(req.url, true);
        if (q.pathname === '/favicon.ico') {
          res.writeHead(200, {'Content-Type': 'image/x-icon'} );
          res.end();
          return;
        }
        const path = q.pathname;
        const queryData = q.query;
        res.write(path);
        res.write(JSON.stringify(queryData));
  
        self.emit('remoteCall', path, JSON.stringify(queryData)); // &lt;-- send to index.js so it can execute temperature read
        self.on('callback',(data)=&gt;{ // &lt;-- HERE callback from index.js with temperature data collected
          console.log(data);
          res.write(data);
          res.end(); // &lt;-- I want to wait for temperature data and then end server response
        }); 
      });
    }
    listen(){
      this.webserver.listen(8080);
      return
    }
  }
  export default Server;
  </code></pre>
  
  <pre><code>/**
   * ./app/probes/tempereture/tempereture.js Temperature module
   */
  const sensor = require('ds18x20');
  
  class Temperature {
    constructor(id) {
      this.id = id;
    }
    async getTemperature() {
      const self = this;
      return new Promise(resolve =&gt; {
        sensor.get(self.id, function (err, tempObj) {
          if (err) { throw err; }
          resolve({id: self.id, value: tempObj});
        });
      });
    }
  }
  export default Temperature;
  </code></pre>
  
  <pre><code>/**
   * ./app/evoirement/pot/pot.js Temperature object parent
   */
  import Temperature from '../../probes/temperature/temperature';
  
  class Pot {
    constructor({potID, waterTemperatureProbeID}) {
      this.id = potID;
      this.waterTemperature = new Temperature(waterTemperatureProbeID);
    }
  }
  export default Pot;
  </code></pre>
  
  <pre><code>/**
   * index.js Main file
   */
  
  import Pot from './app/envoirement/pot/pot';
  import Server from './app/services/server/server';
  
  class Main {
    constructor(){
      this.server = new Server();
      this.appSetup();
    }
    appSetup(){
      const self = this;
      this.pot1 = new Pot({
        potID: 'pot1',
        waterTemperatureProbeID: '28-0114502296aa'
      });
      this.pot2 = new Pot({
        potID: 'pot2',
        waterTemperatureProbeID: '28-0114504ea1aa'
      });
  
      this.server.startServer();
      this.server.listen();
      this.server.on('remoteCall', (path, queryData) =&gt; { // &lt;-- Received a request from http server
        console.log('remoteCall', path, queryData);
        self.pot1.waterTemperature.getTemperature().then(res=&gt;{
          console.log(res);
          this.server.emit('callback', res)
        });
      });
    }
  }
  const app = new Main();
  
  </code></pre>