Created
September 14, 2018 12:24
-
-
Save christhchild28/08d233283730df92757847b7a462d7b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The Instructions: | |
Using Node.js (in Cloud 9), make a request for each of the ten "Meeting List Agenda" pages for Manhattan. Important: show the code for all ten requests. | |
https://parsons.nyc/aa/m01.html | |
https://parsons.nyc/aa/m02.html | |
https://parsons.nyc/aa/m03.html | |
https://parsons.nyc/aa/m04.html | |
https://parsons.nyc/aa/m05.html | |
https://parsons.nyc/aa/m06.html | |
https://parsons.nyc/aa/m07.html | |
https://parsons.nyc/aa/m08.html | |
https://parsons.nyc/aa/m09.html | |
https://parsons.nyc/aa/m10.html | |
Using Node.js: For each of the ten files you requested, save the body as a text file to your "local" environment (either on your own laptop or in AWS Cloud9). | |
We were given some starter code: | |
// npm install request | |
// mkdir data | |
var request = require('request'); | |
var fs = require('fs'); | |
request('https://parsons.nyc/thesis-2018/', function(error, response, body){ | |
if (!error && response.statusCode == 200) { | |
fs.writeFileSync('/home/ec2-user/environment/data/thesis.txt', body); | |
} | |
else {console.log("Request failed!")} | |
}); | |
////////////////////////////////////////////////////////// | |
But I did some research and tried some different solutions. | |
My Code: | |
var request = require('request'); | |
var fs = require('fs'); | |
var options = { | |
hostname: "https://parsons.nyc" | |
port: 443 | |
part: "/aa/m01.html" | |
method: GET | |
}; | |
var req = https.request(options, functions (res) { | |
var responseBody = " "; | |
console.log("Response from server started."); | |
console.log(`Server Status: ${res.statusCode'}) `); | |
console.log("Response Headers: %j", res.headers); | |
res.setEncoding("UTF-8"); | |
res.once("data", function(chunk) { | |
console.log(chunk); | |
}); | |
res.on("data, function("chunk") { | |
console.log(1--chunk-- ${chunk.length}`); | |
responseBody += chunk; | |
}); | |
res.on("end", function() { | |
fs.writeFile("meetinglistagenda.html", responseBody, function) { | |
if(err) { | |
throw err; | |
} | |
console.log("File Downloaded"); | |
}); | |
}); | |
}); | |
req.on("error", function(error) { | |
console.log(`problem with resquest: ${err.message}`); | |
}); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What issue are you getting when you run it ?