/ development

Downloading a file using the node module Request

I needed to download a file using Node and I wasn't sure how to do it. It turns out that its pretty much impossible to search for.

First thing you will need is the request module, npm install --save request. Then this should work:

var fs = require('fs'),
    request = require('request');

function (url, path, callback) {
  request({uri: url})
      .on('close', function() {

This function takes in a url, as a string; the path, also as a string; and a callback function. It will write the file to the specified path and when its complete, it will call your callback function.

I'm much more likely to return a promise instead of a taking a callback parameter but, I thought this might be helpful for more people. I don't know how to handle errors out. If you happen to know, let me know.