How can I replicate the functionality of a wget with node.js?

后端 未结 5 1853
我寻月下人不归
我寻月下人不归 2021-02-02 17:01

Is it possible to essentially run a wget from within a node.js app? I\'d like to have a script that crawls a site, and downloads a specific file, but the href

5条回答
  •  灰色年华
    2021-02-02 17:15

    You can run an external command using child_processes:

    http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback

    var util = require('util'),
        exec = require('child_process').exec,
        child,
        url = 'url to file';
    
    child = exec('wget ' + url,
      function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
          console.log('exec error: ' + error);
        }
    });
    

提交回复
热议问题