Greetings to all. I ask for help in mastering some methods for working with the network.
1. How to get the source code of the page? I make a get request to google site:
var http = require('http'); http.get("http://www.google.com/index.html", function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); });
Writes: Got error: connect EADDRNOTAVAIL Actually, what is the error? Example taken from off docks: http://nodejs.org/api/http.html#http_http_get_options_callback
2. Found another way. I took the code from here: http://nodejs.org/api/http.html#http_http_request_options_callback . The only thing that changed:
var options = { host: 'www.google.com', port: 80, path: '/', method: 'POST' //или GET };
Please clarify what res.statusCode means (where can I get the values of the status codes?), What is written at the end of the example: req.end (); ?
H. On one site I found an example of a simple application. A data entry form is displayed and data is sent to the same page using the POST method. The data acceptance code looks like this: http.createServer(function (req, res) { var pathname = url.parse(req.url).pathname; var postData = ''; req.setEncoding('utf8'); req.addListener('data', function(postDataChunk) { postData += postDataChunk; console.log('Новые данные: ' + postDataChunk); }); req.addListener('end', function() { var post = require('querystring').parse(postData); // операции с данными }); }).listen(1337,'127.0.0.1');
Question: if the data from the form were transmitted by the GET method, how would this code be changed?