Search code examples
javascriptnode.jshttpwebserverget-request

Are get request cached?


I have this nodejs code:

    var http = require('http');
    var options ={
    host: 'www.facebook.com',
    port: 80,
    path: '/',
    method: 'GET'
    };
    console.log("Going to make request...");
    var req = http.get(options, function(response){
    console.log(response.statusCode);
    response.pipe(process.stdout);
    });

It works fine the first time by returning the html. When I change "host:" from facebook.com to google.com I get a 302 error. Why do I have to redirect it? Is the first get request cached somehow?


Solution

  • google.com will trigger a redirect to www.google.com, which is the address they use.

    It has nothing to do with your code, so much as it has to do with the server you are accessing.

    Unless you really want to use http.get() directly, it can be really helpful to use another library from NPM that will simplify the process of getting a message for you (there are a number of options).