Skip to content

Instantly share code, notes, and snippets.

@heldr
Created July 30, 2012 03:51
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 heldr/3204209 to your computer and use it in GitHub Desktop.
Save heldr/3204209 to your computer and use it in GitHub Desktop.
file utils
var request = require('request'),
fs = require('fs');
module.exports = {
getRemoteContent: function( url, cb, browserUserAgent){
var requestConfig = {
uri : url
}
if ( browserUserAgent ) {
requestConfig.headers = {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
}
}
request( requestConfig, function (err, res, content) {
if ( err ) throw err;
if ( res.statusCode == 200 ) {
cb( res, content);
} else {
throw "Error " + res.statusCode;
}
});
},
getRemoteFile: function( url, output, cb ) {
this.getRemoteContent( url, function( res, content ) {
fs.writeFile( output, function(err) {
if ( err ) throw err;
cb({
status: res.statusCode,
output: output
});
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment