-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (34 loc) · 905 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Module dependencies.
*
* @type {exports}
*/
var http = require('http');
var https = require('https');
module.exports = {
/**
* Helper function to download specified url contents.
*
* @param {string} url URL to download
* @param {Function} callback Callback function
*/
download: function download(url, callback) {
var pattern = new RegExp('^https:\/\/', 'i');
var lib = http;
if (pattern.test(url)) {
lib = https;
}
// Fetch URL contents
lib.get(url, function get(res) {
var data = '';
res.on('data', function chunk(chunk) {
data += chunk;
});
res.on('end', function end() {
callback(data);
});
}).on('error', function error() {
callback(null);
});
}
};