我有一个在Heroku上运行的NodeJS服务器(免费版)。服务器接受来自客户端的HTTP POST(传递参数)并在无头浏览器中执行Web请求(使用参数)。无头浏览器被称为HorsemanJS。 “Horseman是一个使用PhantomJS的Node.js模块,它具有直观的可链接API,可理解的控制流,支持多个标签以及内置jQuery。”NodeJS服务器无法处理多个用户
当我从客户端(我的计算机)向服务器发送请求(实际上for
循环的20个请求)到服务器时,服务器代码正常工作(20个HorsemanJS Web请求)并返回期望值。然后,它等待下一个连接。这很好。
问题是,当我尝试用两个不同的客户端(我的电脑和手机)同时连接到服务器时,它崩溃了。我可以重新启动服务器并成功返回到使用一个客户端。我怎样才能让它处理多个客户?坠毁时
错误:从我的服务器代码
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)
提取物:
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);
我试图移动.close
内.then
,也是前.then
。该代码仍然适用于单个客户端,但不是多个。
我怀疑问题是一个客户端在客户端尝试使用它之前/关闭一个PhantomJS实例。
我在使用ZombieJS时也遇到类似问题:http://stackoverflow.com/questions/35563187/zombiejs-intermittently-crashes-when-called-repeatedly-from-a-for-loop – user3320795