Skip to content

Instantly share code, notes, and snippets.

@Macagare
Created March 16, 2013 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Macagare/5176170 to your computer and use it in GitHub Desktop.
Save Macagare/5176170 to your computer and use it in GitHub Desktop.
JS: download file via node.js
var http = require('http');
var url = require('url');
var fs = require('fs');
var XmlStream = require('xml-stream');
var config = require('./config.json'); // already parsed
var downloadFile = function( targetUrl ) {
var xmlItem = "";
var timestamp = parseInt( new Date().getTime() / 1000 );
var options = {
host : url.parse( targetUrl ).host,
port : 80,
path : url.parse( targetUrl ).pathname
};
var downloadFileName = options.path.substring( options.path.lastIndexOf('/') + 1, options.path.length );
var downloadFile = fs.createWriteStream( downloadFileName, {'flags': 'w'});
http.get( options, function(res) {
res.setEncoding('utf8');
var xmlStream = new XmlStream(res);
xmlStream.on('startElement: ' + config.tagname, function(item) {
xmlItem = {};
});
xmlStream.on('updateElement: ' + config.tagname, function(element) {
console.log(element);
xmlItem['tomas_id'] = element.$.ObjectID;
xmlItem['type'] = 'map';
xmlItem['data_source'] = 'tomas';
});
xmlStream.on('text: ' + config.tagname + ' > Latitude', function( element ) {
xmlItem['latitude'] = element.$text;
});
xmlStream.on('text: ' + config.tagname + ' > Longitude', function( element ) {
xmlItem['longitude'] = element.$text;
});
xmlStream.on('endElement: ' + config.tagname, function(item) {
console.log("endElement......");
console.log( xmlItem );
xmlItem = {};
});
// backup data in file
xmlStream.on('data', function(data) {
downloadFile.write(data);
});
// when complete
xmlStream.on('end', function() {
console.log("download end; duration: " + ( parseInt( new Date().getTime() / 1000 ) - timestamp ) + " seconds." );
downloadFile.end();
});
} );
};
http.createServer( function( req, res ) {
if ( req.url === "/" ) {
console.log( config.url );
downloadFile( config.url );
}
res.writeHead( 200, { 'Content-Type' : 'text/plain' } );
res.end( 'Operation successful' );
} ).listen( 1337, '127.0.0.1' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment