title: Can't handle POST requests in node.js
body: <p>I have an assignment to write an <code>http</code> server in <code>node.js</code>, which is why I can't use the <code>http</code> module, as you'll soon see. Anyway, I need to be able to listen to both GET and POST requests. The way I understand it GET only fires <code>data</code> event, and post fires <code>data</code> and <code>end</code>. Here's my code:</p>
  
  <pre><code>function Server(resourceMap, rootFolder) {
  
      this.resourceMap = resourceMap;
      this.rootFolder = rootFolder;
  
      function connectionHandler(socket) {
          console.log('server connected');
          console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
          socket.setEncoding('utf8');
          socket.on('data',function(data) {
                  var re = new RegExp("^( *)(GET)", "i");                 
                  if (data.match(re) != null) {
                      console.log("Handling GET request");
                      router.route(data,socket,handle,resourceMap,rootFolder);
                  }
                  else {
                      (function() {
                          var postData = data;
                          socket.on('data',function(data) {
                              postData += data;
                          });
                          console.log(postData);
                          socket.on('end',function(postData) {
                              console.log("END");
                              console.log("Handling POST request");
                              router.route(postData,socket,handle,resourceMap,rootFolder);
                          });
                      });
                  }
              });
      }
  
      this.server = net.createServer(connectionHandler);
  
      this.port = undefined;
  
      this.startServer = function(port) { //Maybe change backlog for security reasons
          this.port = port;
          this.server.listen(port, function() { //'listening' listener add handle object here
              console.log('server bound');});
      }
  }
  </code></pre>
  
  <p><code>GET</code> requests works just fine. With <code>POST</code> It doesn't even enter the anonymous function.</p>
  
  <p>Any ideas why and how I can solve it?</p>
  
  <p>EDIT: solved - I defined the function but didn't call it :)
  If you have any comments about my design I'd love to hear, as I am new to node.js and JavaSCript</p>
  
  <p>Thanks </p>