1

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?

Eric
  • 25
  • 1
  • 4
  • I found a way to open a new console window through exec (spawn doesn't work) by putting "start" in front of my command. However, doing this breaks the output buffer of the child process. Maybe I'll start a new node instance in the new window to communicate with the node instance that initiated the download. Node doesn't seem to be designed to support multiple console windows or manage windows in any way (unlike WSH). – Eric May 31 '12 at 06:21

1 Answers1

1

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

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • Thanks! But I guess that would leave me with the problem that there is no visible window to display the progress in, since the background process is running in a hidden console. – Eric May 29 '12 at 12:41
  • Thanks. I was hoping to do this only through node, but Node is too limited to do things like opening multiple windows. I ended up starting a second Node instance with its own window. There I'll try some of the things you suggested (using Node's http client instead of WGET and communicate with the other node instance over a TCP port). – Eric May 31 '12 at 06:34