-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathget.js
More file actions
22 lines (19 loc) · 736 Bytes
/
Copy pathget.js
File metadata and controls
22 lines (19 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env node
// http module for sending out our GET request
var http = require('http');
var fs = require('fs');
var url = process.argv[2];
var filename = process.argv[3];
// Standard error handling, in case we forget to put in one of our args
if (!url || !filename) {
console.log("-- error: must supply a URL and a filename as arguments.");
} else {
// Initialize write stream to desired file location
var writeStream = fs.createWriteStream(__dirname + '/' + filename);
http.get(url, function(response){
// Response stream from GET request is piped directly into write stream declared above
response.pipe(writeStream);
}).on('error', function(e){
console.log('Uh oh! Got an error:' + e.message);
});
}