2

I am executing below code snippet but it is not redirecting to actual page.

var http = require('http');

var options = {
  host: 'google.com',
  port: 80,
  path: '/search?q=hello+world'
};

http.get(options, function(resp){
resp.setEncoding('utf8');
  resp.on('data', function(chunk){
    //do something with chunk
console.log('================\n\n', chunk);
  });
}).on("error", function(e){
  console.log("Got error: " + e.message);
});

Command fire :

node sample.js

Output :

================

 <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.in/search?q=hello+world&amp;gws_rd=cr&amp;dcr=0&amp;ei=Wd3xWd_nBITovgSl9bXwCA">here</A>.
</BODY></HTML>

I want to get entire page html but not this instead of I want results for hello world.

artgb
  • 3,177
  • 6
  • 19
  • 36
VVB
  • 7,363
  • 7
  • 49
  • 83

3 Answers3

0

use resp.redirect('url'); for redirect to the required page.

Basically node js is developed for create server.But incidently it has come to server side..so its soo diffcult to make application in core node js..you can try atleast any frameworks such as express js..https://expressjs.com/ just go through this strong & easy & famous framework..if you dont like it then go to another

Janen R
  • 729
  • 10
  • 21
0

Try putting instead of utf8 - “utf-8” ; with the dash sign

Elad Goldenberg
  • 99
  • 1
  • 10
0

What you actually want is to follow the redirect. See this other question for possible answers: How do you follow an HTTP Redirect in Node.js?

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80