NodeJS server can't handle multiple users

好久不见. 提交于 2019-12-10 18:54:40

问题


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 HorsemanJS. "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."

When I send a request (actually for 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.

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?

Error when crashed:

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.<anonymous> (/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)

Extract from my server 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); 

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

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


回答1:


Putting horseman.close() inside of the finally may not give it enough time to actually close our the phantomJS process completely. Try changing the finally into a .then(), and return horseman.close().



来源:https://stackoverflow.com/questions/35306822/nodejs-server-cant-handle-multiple-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!