I use sax-js to read large xml files. I can not get the program to exit when the parser is finished. Here is a script form with parser logic removed.
var fs = require('fs');
var sax = require('sax');
var feedFile = 'foo.xml';
var saxStream = sax.createStream(true)
.on('opentag', function(node) {
.on('end', function() {
console.log("parser end event");
});
var options = {
flags: 'r',
encoding: 'utf8',
mode: 0666,
bufferSize: 1024
};
fs.createReadStream(feedFile, options, function(err) {
throw err;
})
.on('end', function() {
console.log("read stream end event");
})
.pipe(saxStream);
Everything is working correctly. And the console receives messages like this
read stream end event
parser end event
And then the process should exit. Is not. What am I missing?
EDIT: What I miss. There was a resource still open so that node could not exit. The temptation is to start trying to call process.exit () anywhere, anywhere. I tried it myself in this case, and it broke the material.
My script was unable to exit because I had an open mongodb connection (managed by mongoose). Here, essentially (not in detail), what is fixed:
saxStream.on('end', function() {
console.log("parser end event");
mongoose.disconnect();
};