Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js, callback, asynch, commandline
    text
    copied!<p>With the code below I have been trying to download a URL, save to file and than check if there are any html tags in that file (the tags I want to check are in the checks.json). I need to run this from the commandline and for that I think i need the asynch, callback features of the javascript language. </p> <p>And I am lost at this point. I cannot make it work. My problem is that from the commandline I always get the error that the file does not exists. </p> <p>I know it needs to wait until it is downloaded and then run the check. But it seems every time I run the code, that function is not called. And I don't know why. </p> <p>So what I would need to understand is: </p> <p>How I can run this from the commandline, download the URL, save it to file, check with checks.json and print the result to console. </p> <p>Thank you. </p> <pre><code>var fs = require('fs'); var program = require('commander'); var cheerio = require('cheerio'); var rest = require('restler'); var HTMLFILE_DEFAULT = "index.html"; var CHECKSFILE_DEFAULT = "checks.json"; var URLFILE_DEFAULT = "downloaded.html"; var assertFileExists = function(infile) { var instr = infile.toString(); if(!fs.existsSync(instr)) { console.log("%s does not exist. Exiting.", instr); process.exit(1); // http://nodejs.org/api/process.html#process_process_exit_code } return instr; }; var cheerioHtmlFile = function(htmlfile) { return cheerio.load(fs.readFileSync(htmlfile)); }; var loadChecks = function(checksfile) { return JSON.parse(fs.readFileSync(checksfile)); }; var checkHtmlFile = function(htmlfile, checksfile) { $ = cheerioHtmlFile(htmlfile); var checks = loadChecks(checksfile).sort(); var out = {}; for(var ii in checks) { var present = $(checks[ii]).length &gt; 0; out[checks[ii]] = present; } return out; }; var clone = function(fn) { // Workaround for commander.js issue. // http://stackoverflow.com/a/6772648 return fn.bind({}); }; var downAnd2File = function() { rest.get('http://www.wired.com/').on('complete', function(result) { if (result instanceof Error) { sys.puts('Error: ' + result.message); this.retry(5000); // try again after 5 sec } else { fs.writeFile(__dirname + '/downloaded.html', result, function(err) { if (err) throw err; console.log('Saved!'); }); } }); downAnd2File(checkHtmlFile); } if(require.main == module) { program .option('-c, --checks &lt;check_file&gt;', 'Path to checks.json', clone(assertFileExists), CHECKSFILE_DEFAULT) .option('-f, --file &lt;html_file&gt;', 'Path to index.html', clone(assertFileExists), HTMLFILE_DEFAULT) .option('-u, --url &lt;html_file&gt;', 'Path to downloaded url', clone(assertFileExists), URLFILE_DEFAULT) /////////////// .parse(process.argv); var down2FileAndCheck = downAnd2File(checkHtmlFile(program.url, program.checks)); var checkJson = checkHtmlFile(program.file, program.checks); var outJson = JSON.stringify(checkJson, null, 4); console.log(outJson); } else { exports.checkHtmlFile = checkHtmlFile; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload