Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  194] [ 4]  / answers: 1 / hits: 31231  / 12 Years ago, sat, june 16, 2012, 12:00:00

I'm brand new to node.js, but I wanted to play around with some basic code and make a few requests. At the moment, I'm playing around with the OCW search (http://www.ocwsearch.com/), and I'm trying to make a few basic requests using their sample search request:



However, no matter what request I try to make (even if I just query google.com), it's returning 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's going on. I've looked up nginx, but most questions asked about it seemed to be asked by people who were setting up their own servers. I've tried using an https request instead, but that returns an error 'ENOTFOUND'.



My code below:



var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello Worldn');

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 really simple question, and thanks for any help you can give!


More From » node.js

 Answers
48

The problem is that Node.JS's HTTP Request module isn't following the redirect you are given.



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



Basically, you can either look through the headers and handle the redirect yourself, or use one of the handful of modules for this. I've used the request library, and have had good luck with it myself. https://github.com/mikeal/request


[#84869] Thursday, June 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
beatrizrheaq

Total Points: 73
Total Questions: 89
Total Answers: 107

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;