NodeJS服务器无法处理多个用户 [英] NodeJS server can't handle multiple users
问题描述
当我从一个客户端(我的电脑)发送一个请求(实际上 问题是,当我尝试用两个不同的客户端(我的电脑和手机)同时连接到服务器时,它崩溃了。我可以重新启动服务器并成功返回到使用一个客户端。我怎样才能让它处理多个客户? 错误崩溃时: 从我的服务器代码中提取: 我在 我怀疑问题是客户端在尝试使用它之前/之前关闭一个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 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: Extract from my server code: I tried moving 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屋!
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
之前。该代码仍然适用于单个客户端,但不是多个。
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. 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
inside .then
, and also before .then
. The code still worked for a single client, but not multiple.