文件并发出 http 请求"/>
nodejs 读取文件并发出 http 请求
有一个Nodejs脚本可以一个一个读取一组文件。并且对于每个文件,逐行读取文档,读取一行后,它会生成一个http post请求将该行发送到远程服务器。然后阅读下一行。问题是脚本会漏掉一些台词。
谢谢。
似乎
lr.pause();
只是隐藏了line
事件,而不是暂停读取文件过程。
var fs = require('fs');
var http = require('http');
var JSON = require('JSON');
var S = require('string');
var uuid = require('node-uuid');
var readline = require('readline');
var httpsync = require('httpsync');
var LineByLineReader = require('line-by-line');
var sleep = require('sleep');
function postES(_path,data,id,lr){
var post_data = JSON.stringify(data);
var post_options = {
host: _host,
port: _port,
path: _path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
console.log(data);
});
res.on('end', function() {
console.log("end");
// resume read line
lr.resume();
});
});
post_req.on('error', function(data) {
console.log("error,post."+data+post_data);
// resume read line
lr.resume();
});
post_req.write(post_data);
post_req.end();
}
function readlineFunSession(line,id,lr) {
var _data={};
// compose _data object
postES('/cs/session/'+_data["sessionid"],_data,id,lr);
}
function readfileFun(files,start,end,id,readlineFun) {
if(start<end && start<files.length){
var lr = new LineByLineReader(files[start],{encoding:'utf8',skipEmptyLines:true});
lr.on('error', function (e) {
console.log('error,LineByLineReader.'+e.toString());
});
lr.on('line', function (line) {
// pause read line
lr.pause();
try{
readlineFun(line,id,lr);
}catch(e){
console.log('error,line.'+e.toString());
}
});
lr.on('end', function () {
readfileFun(files,++start,end,id,readlineFun);
});
}
}
// var files is an arry of files
// this function try to go throgh file[0],file[1],file[2],......,file[10],
readfileFun(files,0,10,"ID-1",readlineFunSession);
回答如下:
执行一系列操作,其中下一个操作应该在 nodejs 中只有当前完成后运行,由于其异步范例,有点困难,您可以做的一种方法是使用同步制造商 npm,如光纤或瀑布,
但是你可以做的其他简单(和愚蠢)的方法是创建虚拟工作管理器,让你的 nodejs 无限运行,而每个(时间间隔),检查当前进度是否完成,如果完成则运行下一个动作。
顺便说一句,虽然你不能请求同步,但你可以同步读取文件,所以在你的情况下,我认为你应该读取所有文件中的所有行以成为一个大的行数组。
var jswget = require("jswget");
var arrayoflines = ["line1", "line2", "line3"];
var counter = 0;
var inProgress = false;
var request = function(){
if (arrayoflines.length == 0) {
// no more line, should exit
process.exit();
}
if (inProgress) {
// if previous work is not completed then skip.
return;
}
// get first line, and remove it from array index
var current_line = arrayoflines.shift();
inProgress = true;
jswget({
url: "http://someurl:3000/somepath?q1=" + current_line,
method: 'POST',
formdata: some_postdata,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
},
onsuccess: function(responsetext, req, res){
// success requesting, should do next line
},
onerror: function(err, req){
// oops, error occurred, but we will do next line nevertheless
},
onend: function(){
// success or not, the request is end, so we should prepare for next request
counter+=1;
inProgress = false;
}
})
}
setInterval(function(){
request();
}, 100)
更多推荐
nodejs 读取文件并发出 http 请求
发布评论