執行代碼我有一些解析PCAP文件和解析的數據寫入到一個數組data = []
然後代碼將這些數據寫入到一個文件JSON:Node.js的特定的順序
var fs = require("fs");
var pcapp = require('pcap-parser');
var hex = "";
var data = [];
var i = 0;
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
var parser = pcapp.parse('C:/Users/Brandt Winkler Prins/Desktop/data.pcap');
parser.on('packet', function(packet) {
hex = [...packet.data].map(_ => ('0' +_.toString(16)).slice(-2)).join();
var ticker = hex2a(hex.substring(282, 306).replace(/,/g,""));
var bidPrice = parseInt(hex.substring(318,342).split(",").reverse().join(""), 16);
var timestamp = parseInt(hex.substring(258, 282).split(",").reverse().join(""), 16);
if(packet.header.capturedLength == 126 && ticker == "AAPL ") {
data[2*i] = bidPrice;
data[2*i-1] = timestamp;
i++
}
});
data = JSON.stringify(data);
fs.writeFile("C:/Users/Brandt Winkler Prins/Desktop/Project/data.json",data);
所有的數據被寫入到其相應的陣列parser.on(...)
,但是node.js執行我的.writeFile
命令,然後執行parser.on(...)
。總而言之,我的問題是:如何在寫入文件之前強制節點執行我的代碼?
任何幫助,非常感謝。
後提交只想說感謝並獲得評論員徽章 – BWP