title: TypeError: Cannot read property 'headers' of undefined on Node.Js
body: <p>I'm trying to do a simple form submission with Node.js and Express, it's my first time working with this technologies and to be honest I'm stuck.</p>
  
  <p>I got 3 javascript scripts and 1 html page. One of the script is Index.js, where I'm running the server with node.js and express, other is to verify the input on the html and an Ajax call with XMLHttpRequest object if all the data is correct, and the last one is the one who handle the HTTP request and saves the input data on a .json file.</p>
  
  <p>At this moment I'm yet not saving the input data in the .json file.</p>
  
  <p>The problem I'm facing is I'm getting the next error: 
  <strong>TypeError: Cannot read property 'headers' of undefined</strong></p>
  
  <p>I don't know how to solve it but I know the error is in the script which manage the HTTP request to save it on the json file.</p>
  
  <p>This is the code which trows error:</p>
  
  <pre><code>
  const https = require('https');
  const fs = require('fs');
  const parse = require('querystring');
  
  
  class SignUp {
  
    collect(req, res) {
      collectReqData(req, res =&gt; {
        console.log(res);
      });
  
      function collectReqData(request, callback) {
        const contentType = res.headers['content-type'];
        const DATA_FORM = 'multipart/form-data';
  
        if(contentType === DATA_FORM) {
  
          let body = '';
  
          request.on('data', chunk =&gt; {
            body += chunk.toString();
          });
  
          request.on('end', () =&gt; {
            callback(parse(body));
          });
  
        }
        else{
          callback(null);
        }
      }
  
    }
  
  }
  
  var newRequest = new SignUp();
  
  newRequest.collect();
  
  module.exports = SignUp;
  </code></pre>
  
  <p>Next the Ajax call on the verification script:</p>
  
  <pre><code>    let formElement = document.getElementById('myForm');
      var request = new XMLHttpRequest();
      request.open('POST', './signUp.js');
      request.send(new FormData(formElement));
  </code></pre>
  
  <p>And then Index.js file:</p>
  
  <pre><code>'use strict';
  
  const fs = require('fs');
  const path = require('path');
  const express = require('express');
  const app = express();
  
  app.get('/', (req, res) =&gt; {
    res.sendFile(path.join(__dirname,'index.html')); 
  });
  
  app.get('/submit.js', (req, res) =&gt; {
    let k = fs.readFileSync('./submit.js', 'utf8');
    res.send(k);
  });
  
  app.post('/signUp.js', (req, res) =&gt; {
    let signUp = require('./signUp');
    res.sendStatus(200);
  });
  
  app.listen(3000, () =&gt; {
    console.log('server started');
  });
  
  </code></pre>
  
  <p>Please if someone could help me and explain what am I doing wrong I'll be really thankful, and I'll also appreciate any suggestions on my code because I'm new which this technology and it will be really helpful.</p>