質問

I'm writing a node.js script that permanently runs in the background and initiates file downloads from time to time. I want to use WGET for downloading the files, since it seems more robust until I really know what I'm doing with node.js.

The problem is that I want to see the progress of the downloads in question. But I can't find a way to start WGET through node.js in a way that a new shell window is opened and displayed for WGET.

Node's exec and spawn functions run external commands in the background and would let me access the output stream. But since my script runs in the background (hidden), there's not much I could do with the output stream.

I've tried opening a new shell window by running "cmd /c wget..." but that didn't make any difference.

Is there a way to run external command-line processes in a new window through node.js?

役に立ちましたか?

解決

You can use node-progress and Node's http client (or requesT) for this, no need for wget:

https://github.com/visionmedia/node-progress

Example:

var ProgressBar = require('../')
  , https = require('https');

var req = https.request({
    host: 'download.github.com'
  , port: 443
  , path: '/visionmedia-node-jscoverage-0d4608a.zip'
});

req.on('response', function(res){
  var len = parseInt(res.headers['content-length'], 10);

  console.log();
  var bar = new ProgressBar('  downloading [:bar] :percent :etas', {
      complete: '='
    , incomplete: ' '
    , width: 20
    , total: len
  });

  res.on('data', function(chunk){
    bar.tick(chunk.length);
  });

  res.on('end', function(){
    console.log('\n');
  });
});

req.end();

UPDATE:

Since you want to do this in a background process and listen for the download progresses (in a separate process or what have you) you can achieve that using a pub-sub functionality, either:

  • use a message queue like Redis, RabbitMQ or ZeroMQ
  • start a TCP server on a known port / UNIX domain and listen to it

Resources:

http://nodejs.org/api/net.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top