所以我的问题是,当我向运行casperjs scraper的wesbervice发送多个请求时(使用类似postman的东西),当我尝试将响应写回客户端时,它崩溃了,因为(我认为)响应从在第二个请求之前的第一个请求有时间完成。这是我的代码的简化版:
var server = require('webserver').create();
var fs = require('fs');
var utils = require('utils');
var adminLogin = JSON.parse(fs.read('./config.json'));
var casper = require('casper').create({
pageSettings: {
loadImages: true,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0'
},
clientScripts: [
'./node_modules/jquery/dist/jquery.min.js'
]
});
var ip_server = '127.0.0.1:3000';
var showMoreSelector = "a.button";
function step() {
...
}
var service = server.listen(ip_server, function (req, res) {
var results;
var parameters = req.url;
var userID = parameters.substring(parameters.indexOf("?user=") + 6);
casper.start('https://someurl.com')
.then(function(){
...
});
casper.wait(2000, function() {
...
})
.thenOpen("https://someurl", function() {
...
})
.then(step)
.then(function() {
...
});
casper.run(function() {
//HERE is where the issue lies when sending multiple requests at once
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.write(JSON.stringify(results));
res.close();
});
})
console.log('Server running at http://' + ip_server+'/');
这是第二个请求尝试完成时出现的错误:
Error: cannot access member `statusCode' of deleted QObject
所以基本上我的问题是,我该如何做,保持响应打开,直到所有请求都完成?还是有更好的方法来应对这个问题?