title: Secure POST request from NodeJS to Node/Express hangs
body: <p>I'm using the following to send POST data to a secure nodejs server:</p>
  <p>File: main.js</p>
  <pre><code>    var strdata = JSON.stringify({&quot;data&quot;:&quot;thisdata&quot;});
  
      var options = {
          host: '192.168.1.63',
          port: 3001,
          path: '/saveconfig',
          method: 'POST',
          rejectUnauthorized: false,
          requestCert: true,
          agent: false,
          
          headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': Buffer.byteLength(strdata)
          }   
          
      };
  
      
      var req = https.request(options, function(res) {
          console.log('STATUS: ' + res.statusCode);
          console.log('HEADERS: ' + JSON.stringify(res.headers));
          res.setEncoding('utf8');
          res.on('data', function (chunk) {
              console.log('BODY: ' + chunk);
          });
      });
      
      console.log(req.write(strdata));
      console.log(req.end());
  
      req.on('error', function(e) {
          console.log('problem with request: ' + e.message);
      });
  
      req.on('finish', function() {
          console.log(&quot;finished request&quot;);
      });
  </code></pre>
  <p>In an otherwise functional expressjs server, these are the appropriate snippets:</p>
  <p>File: app.js</p>
  <pre><code>app.post('/saveconfig', function() { 
      data.saveconfig; console.log(&quot;received request&quot;); } );
  
  app.get('/getconfig', data.getconfig);
  </code></pre>
  <p>File: data.js</p>
  <pre><code>exports.saveconfig = function(req, res) {
  console.log(&quot;saveing config&quot;);
          
  res.send(200);
  res.end();
  
  };
  
  exports.getconfig = function(req, res) {
  res.send(&quot;get OK&quot;);
  }
  </code></pre>
  <p>With app.js running on the server (Ubuntu), I run main.js from the client (Windows 7). req.write and req.end execute and &quot;finished request&quot; logs to the console, but the request callback never fires.</p>
  <p>On the server in app.js, the app.post event fires and logs &quot;received request&quot; to the console. But &quot;saving config&quot; never logs to the console. Only after I kill (^C) main.js, express then logs to the console &quot;POST /saveconfig&quot;.</p>
  <p>I know I'm missing something simple, but I've read dozens of coding examples and you can likely gather from my snippet, I've tried everything I can find or think of. I'd guess the request isn't finishing, but I don't know why. What is missing to get &quot;exports.saveconfig&quot; to fire?</p>
  <p><em>additional information</em></p>
  <p><strong>The answer posted below fixed my problem. Because I'm new to stackoverflow, I can't post my own answer, but here's the rest of the story...</strong></p>
  <p>I appreciate your help. Being still new to JavaScript, I found I can learn a lot about an object by converting it to string. I was originally attempting to convert the req parameter to a string <em>using a custom function</em>. I just discovered it was apparently running into an endless loop after using JSON.stringify instead.</p>
  <p>The code looked something like this:</p>
  <pre><code>exports.saveconfig = function (db) {
      return function(req, res) {
          console.log(&quot;saving config&quot;);
      
          console.log(mymodule.serialize(req));
      
          res.end(&quot;OK&quot;);
      
          console.log(req.body);
     };
  };
  </code></pre>
  <p>I would have thought the above code should have logged the following to the console- even if the serialize method was in an endless loop:</p>
  <blockquote>
  <p>POST /saveconfig</p>
  <p>saving config</p>
  <p>[nothing because of the endless loop]</p>
  </blockquote>
  <p>Instead I got:</p>
  <blockquote>
  <p>saving config</p>
  <p>connections property is deprecated. Use getConnections() method</p>
  </blockquote>
  <p>Being new to JavaScript, I assumed something was wrong with the request, the server, or some plumbing in-between. Of course, then the debugging code I added (along with my ignorance of JS) compounded the problem.</p>
  <p>Changing</p>
  <pre><code>app.post('/saveconfig', function() { 
  data.saveconfig; console.log(&quot;received request&quot;); } );
  </code></pre>
  <p>to</p>
  <pre><code>app.post('/saveconfig', datarts.saveconfig);
  </code></pre>
  <p>and removing the endless loop fixed the problem.</p>