NodeJS server can't handle multiple users
<p>I have a NodeJS server running on Heroku (free version). The server accepts a HTTP POST from a client (passing a parameter) and does a web request (using the parameter) in a headless browser. The headless browser is called <a href="http://www.horsemanjs.org" rel="nofollow">HorsemanJS</a>. "Horseman is a Node.js module that makes using PhantomJS a pleasure. It has a straight-forward chainable API, understandable control-flow, support for multiple tabs, and built-in jQuery." </p>

<p>When I send a request (actually <code>for</code> loop of 20 requests) from a client (my computer) to the server, the server code works correctly (does 20 HorsemanJS web requests), and returns the expected values. Then, it waits for the next connection. This is all good. </p>

<p>The problem is, when I try to connect to the server with two different clients (my computer and phone) at the same time, it crashes. I can reboot the server and return to using one client successfully. How can I make it handle multiple clients? </p>

<p>Error when crashed:</p>

<pre><code>child_process.js:788
child.stdout.addListener('data', function(chunk) {
             ^  
TypeError: Cannot read property 'addListener' of undefined
     at Object.exports.execFile (child_process.js:788:15)
     at exports.exec (child_process.js:649:18)
     at Socket.&lt;anonymous&gt; (/app/node_modules/node-horseman/node_modules/node-phantom-simple/node-phantom-simple.js:237:7)
     at Socket.g (events.js:199:16)
     at Socket.emit (events.js:107:17)
     at readableAddChunk (_stream_readable.js:163:16)
     at Socket.Readable.push (_stream_readable.js:126:10)
     at Pipe.onread (net.js:538:20)
</code></pre>

<p>Extract from my server code:</p>

<pre><code>var express = require('express');
var app = express();
var Horseman = require('node-horseman'); 

app.set('port', (process.env.PORT || 5000));

var printMessage = function() { console.log("Node app running on " + app.get('port')); };

var getAbc = function(response, input)
{
    var horseman = new Horseman();

    horseman
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0")  // building web browser
        .open('http://www.stackoverflow.com')                        
        .html()                                     
        .then(function (result) {
            var toReturn = ijk(result));

            response.writeHead(200, {'Content-Type': 'text/plain'});
            response.end(toReturn);
        }).close();
}

var handleXyz = function(request, response)
{
    getAbc(response, request.query.input); 
}

app.listen(app.get('port'), printMessage); 
app.post('/xyz', handleXyz); 
</code></pre>

<p>I tried moving <code>.close</code> inside <code>.then</code>, and also before <code>.then</code>. The code still worked for a single client, but not multiple. </p>

<p>I suspect the problem is that one client is closes a PhantomJS instance while/before the client tries to use it. </p>