Node.js Requests Returning 301 Redirects

I am new to node.js, but I wanted to play around with some basic code and make some requests. Right now I'm playing with OCW search ( http://www.ocwsearch.com/ ), and I'm trying to make some basic queries using my sample search query:

However, no matter what request I try to make (even if I just request google.com), it returns me

<html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/0.7.65</center> </body> </html> 

I'm not too sure what is going on. I was looking for nginx, but most of the questions were asked about this, it seemed people were asking who set up their own servers. Instead, I tried using the https request, but this returns an "ENOTFOUND" error.

My code is below:

 var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); var options = { host:'ocwsearch.com', path: '/api/v1/search.json?q=statistics&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/', method: 'GET' } var req = http.request(options, function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }); req.end(); req.on('error', function(e) { console.error(e); }); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/'); 

Sorry if this is a very simple question and thanks for any help you can give!

+6
source share
4 answers

The problem is that the Node.JS HTTP Request module does not perform the redirect you give.

See this question for more details: How do you follow HTTP redirects in Node.js?

Basically, you can view the headers and handle the redirection yourself, or use one of several modules for this. I used the query library, and I was lucky too. https://github.com/mikeal/request

+5
source
 var http = require('http'); var find_link = function(link, callback){ var root =''; var f = function(link){ http.get(link, function(res) { if (res.statusCode == 301) { f(res.headers.location); } else { callback(link); } }); } f(link, function(t){i(t,'*')}); } find_link('http://somelink.com/mJLsASAK',function(link){ console.log(link); }); function i(data){ console.log( require('util').inspect(data,{depth:null,colors:true}) ) } 
+2
source

This question is old, but I got the same error 301, and these answers didn't really help me solve the problem.

I wrote the same code:

 var options = { hostname: 'google.com', port: 80, path: '/', method: 'GET', headers: { 'Content-Type': 'text/plain', } }; var http = require('http'); var req = http.request(options, function(res) { console.log('STATUS:',res.statusCode); console.log('HEADERS: ', JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function(chunk) { console.log(chunk); }); res.on('end', function() { console.log('No more data in response.'); }); }); req.on('error', function(e) { console.log('problem with request: ', e.message); }); console.log(req); req.end(); 

so after a while I realized that there is a really tiny bug in this code that is part of the host name:

  var options = { hostname: 'google.com', ... 

you need to add "www". get html content in front of your url, otherwise there will be a 301 error.

 var options = { hostname: 'www.google.com', 
+1
source

For me, the site where I tried GET redirected me to a secure protocol. Therefore i changed

 require('http'); 

in

 require('https'); 
0
source

Source: https://habr.com/ru/post/918232/


All Articles