title: Node: Cannot set headers after they are sent to the client
body: <p>I did some research on the following error:</p>
  <pre><code>Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
  </code></pre>
  <p>I understood the idea and what's causing it but I am not sure how to fix it for my controller code:</p>
  <pre><code>exports.isServerOnline = (req, res, next) =&gt; {
      const sock = new net.Socket();
      sock.setTimeout(2500);
      sock.on('connect', function () {
          sock.destroy();
          return res.json({
              &quot;server&quot;: &quot;online&quot;
          });
      }).on('error', function (e) {
          return res.json({
              &quot;server&quot;: &quot;offline&quot;
          });
      }).on('timeout', function (e) {
          return res.json({
              &quot;server&quot;: &quot;offline&quot;
          });
      }).connect(LS_PORT, LS_HOST);
  }
  </code></pre>
  <p>I'm using socket to check if a remote host is up or down. Here is my client code (Vue.js):</p>
  <pre><code>created() {
      this.getServerStatus();
    },
  
    methods: {
      getServerStatus() {
        axios
          .get(`${process.env.VUE_APP_BACKEND_URL}/server/status`)
          .then(response =&gt; {
            if (response.data.server === &quot;online&quot;) {
              // no interesting code here
            } else {
              // no interesting code here
            }
            /* eslint-disable no-console */
          })
          .catch(error =&gt; {
            console.log(error);
          });
      }
    }
  </code></pre>
  <p>Complete error stack trace:</p>
  <pre><code>2020-07-02T07:40:47.743193+00:00 app[web.1]: _http_outgoing.js:518
  2020-07-02T07:40:47.743201+00:00 app[web.1]: throw new ERR_HTTP_HEADERS_SENT('set');
  2020-07-02T07:40:47.743202+00:00 app[web.1]: ^
  2020-07-02T07:40:47.743202+00:00 app[web.1]:
  2020-07-02T07:40:47.743204+00:00 app[web.1]: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
  2020-07-02T07:40:47.743205+00:00 app[web.1]: at ServerResponse.setHeader (_http_outgoing.js:518:11)
  2020-07-02T07:40:47.743206+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
  2020-07-02T07:40:47.743206+00:00 app[web.1]: at ServerResponse.json (/app/node_modules/express/lib/response.js:264:10)
  2020-07-02T07:40:47.743206+00:00 app[web.1]: at Socket.&lt;anonymous&gt; (/app/src/controllers/status.js:15:20)
  2020-07-02T07:40:47.743207+00:00 app[web.1]: at Socket.emit (events.js:315:20)
  2020-07-02T07:40:47.743207+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:92:8)
  2020-07-02T07:40:47.743208+00:00 app[web.1]: at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
  2020-07-02T07:40:47.743208+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  2020-07-02T07:40:47.743209+00:00 app[web.1]: code: 'ERR_HTTP_HEADERS_SENT'
  2020-07-02T07:40:47.743209+00:00 app[web.1]: }
  </code></pre>
  <p>I would highly appreciate if someone could point me on the right track.</p>