var fs = require('fs');
var http = require('http');
var url = require('url');
var querystring = require('querystring');
var child_process = require('child_process');

var host = '0.0.0.0'
var port = process.env.PORT || 8779;

var wkhtmltopdf = 'wkhtmltopdf' // command to execute
var documentation = fs.readFileSync('Readme.md');

function handleHtmlToPdf(html, res) {
  var child = child_process.spawn(wkhtmltopdf, ['-', '-']);
  var buffers = [];
  child.stdout.on('data', function(data) { buffers.push(data) });
  child.on('exit', function(code) {
    if (code === 0) {
      var buffer = Buffer.concat(buffers);
      res.writeHead(200, {
        'Content-Type': 'application/pdf', 
        //'Content-Length': buffer.length
      });
      res.end(buffer);
    } else {
      console.log('Error while running wkhtmltopdf: Error ' + code);
        res.writeHead(500, {'Content-Type': 'text/plain'});
      res.end('Error while running wkhtmltopdf');
    }
  });
  child.stdin.on('error', function() { });
  child.stdin.end(html);
}

exports.server = http.createServer(function (req, res) {
  u = url.parse(req.url);
  if (u.pathname == '/') {
    if (req.method != 'GET') {
      res.writeHead(405, {'Content-Type': 'text/plain'});
      res.end('Only GET method allowed');
      return;
    }
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(documentation);
  } else if (u.pathname == '/pdf') {
    if (req.method != 'POST') {
      res.writeHead(405, {'Content-Type': 'text/plain'});
      res.end('Only POST method allowed');
      return;
    }
    var body = '';
    req.on('data', function(data) { body += data });
    req.on('end', function() {
      var post = querystring.parse(body);
      var html = post.html;
      if (html) {
        handleHtmlToPdf(html, res);
      } else {
        res.writeHead(400, {'Content-Type': 'text/plain'});
        res.end('Form parameter "html" required');
      }
    });
  } else {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('Not found');
  }
}).listen(port, host);

console.log('Listening on http://'+host+':'+port);


@@ -1159,17 +1159,17 @@
 .method 
-!
+=
 = 'GET')
@@ -1183,35 +1183,35 @@
   res.writeHead(
-405
+200
 , %7B'Content-Type
@@ -1247,56 +1247,39 @@
 end(
-'Only GET method allowed');%0A      return;%0A    %7D%0A
+documentation);%0A    %7D else %7B%0A  
     
@@ -1284,35 +1284,35 @@
   res.writeHead(
-200
+405
 , %7B'Content-Type
@@ -1330,32 +1330,34 @@
 ain'%7D);%0A    
+ 
+ 
 res.end(
 documentatio
@@ -1344,31 +1344,49 @@
 res.end(
-documentation);
+'Only GET method allowed');%0A    %7D
 %0A  %7D els
@@ -1438,9 +1438,9 @@
 hod 
-!
+=
 = 'P
@@ -1453,127 +1453,8 @@
 %7B%0A  
-    res.writeHead(405, %7B'Content-Type': 'text/plain'%7D);%0A      res.end('Only POST method allowed');%0A      return;%0A    %7D%0A
     
@@ -1468,16 +1468,18 @@
 y = '';%0A
+  
     req.
@@ -1523,24 +1523,26 @@
 ata %7D);%0A    
+  
 req.on('end'
@@ -1558,24 +1558,26 @@
 n() %7B%0A      
+ 
+ 
 var post = q
@@ -1602,24 +1602,26 @@
 ody);%0A      
+  
 var html = p
@@ -1630,16 +1630,18 @@
 t.html;%0A
+  
       if
@@ -1658,16 +1658,18 @@
         
+  
 handleHt
@@ -1690,24 +1690,26 @@
 res);%0A      
+  
 %7D else %7B%0A   
@@ -1701,24 +1701,26 @@
    %7D else %7B%0A
+  
         res.
@@ -1771,24 +1771,26 @@
 %7D);%0A        
+  
 res.end('For
@@ -1830,18 +1830,142 @@
     
+  
 %7D%0A    
+ 
+ 
 %7D);%0A
+    %7D else %7B%0A      res.writeHead(405, %7B'Content-Type': 'text/plain'%7D);%0A      res.end('Only POST method allowed');%0A    %7D%0A
   %7D 

use positive conditions, don't use explicit returns.