NodeJS服务器无法处理多个用户 [英] NodeJS server can't handle multiple users

查看:131
本文介绍了NodeJS服务器无法处理多个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Heroku上运行的NodeJS服务器(免费版)。服务器接受来自客户端的HTTP POST(传递参数)并在无头浏览器中执行Web请求(使用参数)。无头浏览器称为 HorsemanJS 。 Horseman是一个使用PhantomJS的Node.js模块,它具有直观的可链接API,可理解的控制流,支持多个标签以及内置jQuery。



当我从一个客户端(我的电脑)发送一个请求(实际上 for 20个请求的循环)到服务器,服务器代码正常工作(20个HorsemanJS Web请求),并返回预期值。然后,它等待下一个连接。这很好。

问题是,当我尝试用两个不同的客户端(我的电脑和手机)同时连接到服务器时,它崩溃了。我可以重新启动服务器并成功返回到使用一个客户端。我怎样才能让它处理多个客户?



错误崩溃时:

  child_process.js:788 
child.stdout.addListener('data',function(chunk){
^
TypeError:无法读取Object.exports.execFile中未定义
的属性'addListener'(child_process.js :788:15)
在exports.exec(child_process.js:649:18)
在Socket。< anonymous>(/ app / node_modules / node-horseman / node_modules / node-phantom-simple /node-phantom-simple.js:237:7)Socket.g(events.js:199:16)
Socket.emit(events.js:107:17)

at readAddChunk(_stream_readable.js:163:16)
在Socket.Readable.push(_stream_readable.js:126:10)
在Pipe.onread(net.js:538:20)

从我的服务器代码中提取:

  var express = require('express'); 
var app = express();
var Horseman = require('node-horseman');

app.set('port',(process.env.PORT || 5000));
$ b $ var printMessage = function(){console.log(Node app running on+ app.get('port')); };

var getAbc =函数(响应,输入)
{
var horseman = new Horseman();
$ b $ horseman
.userAgent(Mozilla / 5.0(Windows NT 6.1; WOW64; rv:44.0)Gecko / 20100101 Firefox / 44.0)//建立网页浏览器
.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);

我在 .close c $ c> .then ,还有 .then 之前。该代码仍然适用于单个客户端,但不是多个。



我怀疑问题是客户端在尝试使用它之前/之前关闭一个PhantomJS实例。

解决方案

将horseman.close()放在finally中可能没有足够的时间来完全关闭我们的phantomJS过程。尝试将finally改为.then(),然后返回horseman.close()。


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.

解决方案

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

这篇关于NodeJS服务器无法处理多个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆