title: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - Nodejs, Nginx, SSL, Unbuntu
body: <p>Some posts address the solution to add a return statement to the request handlers. I tried to apply this advice without success.
  when running server.js, I am getting the data of the GET request before the error is thrown. In the browser application, only the property (q.questionid and q.question) is displayed.</p>
  <p>Before I switched my webserver from Apache2 to Nginx and implemented SSL, the code worked fine. I use Nginx as a reverse proxy for my Nodejs Express app which queries Mysql on the same host.</p>
  <p>What am I missing?</p>
  <p>This is the log I get when running server.js. First the data and then the error message:</p>
  <pre><code>This app listens port 3000.
  Connected to MySQL server
  output:  [
    {
      questionid: 1,
      question: &quot;What's your age?&quot;,
      unit: 'years',
      images: [ [Object] ]
    },
  // ... the rest of the object
  ]
  node:_http_outgoing:573
      throw new ERR_HTTP_HEADERS_SENT('set');
      ^
  
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at new NodeError (node:internal/errors:371:5)
      at ServerResponse.setHeader (node:_http_outgoing:573:11)
      at ServerResponse.header (/var/www/maraxai.de/html/HSF/calculator/report/node_modules/express/lib/response.js:776:10)
      at ServerResponse.send (/var/www/maraxai.de/html/HSF/calculator/report/node_modules/express/lib/response.js:170:12)
      at ServerResponse.json (/var/www/maraxai.de/html/HSF/calculator/report/node_modules/express/lib/response.js:267:15)
      at createArray (/var/www/maraxai.de/html/HSF/calculator/report/server.js:117:18)
      at Query.onResult (/var/www/maraxai.de/html/HSF/calculator/report/server.js:86:13)
      at /var/www/maraxai.de/html/HSF/calculator/report/node_modules/mysql2/lib/commands/query.js:86:16
      at processTicksAndRejections (node:internal/process/task_queues:78:11) {
    code: 'ERR_HTTP_HEADERS_SENT'
  </code></pre>
  <p>Here is my server.js code:</p>
  <pre><code>var app = express()
      .use(logger('common'))
      .use(express.json())
      .use(express.urlencoded({ extended: true }))
      .use(cors())
      .use(function (req, res, next) {
          res.header(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);
          res.header(&quot;Access-Control-Allow-Headers&quot;, &quot;Origin, X-Requested-With, Content-Type, Accept&quot;);
          res.header(&quot;Access-Control-Allow-Methods&quot;, &quot;GET, PUT, POST, DELETE&quot;);
          next();
      });
  
  var connection = mysql.createConnection({
      host:'localhost',
      socketPath:'/var/run/mysqld/mysqld.sock',
      database: 'dbName',
      user: 'user',
      password: 'password',
  });
  
  connection.connect(function (error) {
      if (error) {
          console.log('error in connection: ', error);
      }
      console.log('Connected to MySQL server');
  });
  
  
  app.get('/HSF/calculator/quiz', function (request, response) {
      var params = request.body;
      connection.query(&quot;\n\t\t\tSELECT q.questionid, q.question, q.unit, i.path, i.title, i.subtitle\n\t    FROM questions AS q\n\t\t\tLEFT JOIN images AS i ON q.questionid = i.questionid\n      ORDER BY q.questionid, i.questionid\n\t&quot;, function (error, rows, fields) {
          if (!error) {
              response.json(rows)
              createArray(rows);
              return;
          }
          else {
              console.log(error);
              return response.status(404).send('something is wrong here');
          }
      });
      function createArray(rows) {
          var output = [];
          rows.forEach(function (entry) {
              //console.log(entry);
              var questionid = entry.questionid;
              var question = entry.question;
              var unit = entry.unit;
              var images = {
                  &quot;path&quot;: entry.path,
                  &quot;title&quot;: entry.title,
                  &quot;subtitle&quot;: entry.subtitle
              };
              if (output[questionid] === undefined) {
                  output[questionid] = {
                      &quot;questionid&quot;: entry.questionid,
                      &quot;question&quot;: entry.question,
                      &quot;unit&quot;: entry.unit,
                      &quot;images&quot;: []
                  };
              }
              output[questionid].images.push(images);
          });
          output.shift();
          console.log('output: ', output);
          response.json(output);
          return output;
      }
  });
  
  app.listen(3000, function () {
      console.log('This app listens port 3000.');
  });
  
  </code></pre>