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?



    1 answer 1

    1) everything works fine (and not with examples of the level 'hello node.js', what is your version of the node? Is the second option working?)

    And get the contents like this:

     //..... function( response ){ var pageHtml = ''; response .on('data', function( chunk ){ pageHtml += chunk } .on('end', function() { console.log( pageHtml ); // pageHtml - requested page content }) } //..... 

    2) statusCode - the response code for your HTTP request. You can find status codes in Google

    ( for the lazy ).

    3)

     var url = require('url'); http.createServer(function( req, res ) { var GET = url.parse(req.url, true).query; // .... }) 
    PS: A small wish - don't ask a few diverse questions in one, maybe someone, for example, will have a question of parsing GET parameters and he will not be able to find an answer, because you must agree - the title of the question does not contribute to this
    PPS: On the rights of trolling and provocation - return <small> and <strike>: P
    • Version of the node 0.6.18. The second option works. I was not looking for status codes, because I thought they were specific to the node, and I practically didn’t work with http on other yaps (3) But I didn’t think of it myself (!?!) But what happens if the het request was executed via ajax? URL of the page may not change, what to do in this case? "PS: A small wish - don't ask a few different questions in one", well) I just got used to other formats of forums where you were welcome to ask all the necessary questions concerning this topic in one topic .. - LightShock
    • @LightStock - so off of dock 0.8+. Either upgrade to the latest stable version, or read the current dock for your nodejs.org/docs/v0.6.18/api version. "The URL of the page may not change" - do not blunt. If you send an ajax GET request, when passing parameters, the page URL changes even if the code looks like this: $ .get ('', {p1: '...', p2: '...'}, function () {/*...*/}) In fact, the url will be requested: ' site /? P1 = ... & p2 = ...', so do nothing :) PS.: The questions are not entirely on the same subject, I gave an example - Zowie
    • You can answer for "What the end of the example says: req.end ();?" to the second question? ZY thanks, lately you often help out) - LightShock
    • one
      req.end definitely worth a separate question. In short, node.js uses http 1.1 and chunked transfer encoding, respectively, there is no content-length in the node, etc. (which eliminates the need to buffer all html before responding to the client), req.end is needed for if the server could "tell the browser" that "it has nothing more to say" :) (explained as simply and briefly as possible) - Zowie
    • Thanks again) - LightShock