2012-06-16 127 views
6

我是node.js的新手,但我想玩弄一些基本代碼併發出一些請求。目前,我正在玩OCW搜索(http://www.ocwsearch.com/),我正在嘗試使用他們的示例搜索請求提出幾個基本請求:Node.js返回301重定向的請求

但是,無論我嘗試提出什麼要求(甚至如果我只是查詢google.com),它返回我

<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> 

我也不太清楚是怎麼回事。我查閱了nginx,但大多數問題都是關於它的問題,似乎是由建立自己的服務器的人提出的。我已經嘗試使用https請求,但是會返回錯誤「ENOTFOUND」。

我下面的代碼:任何幫助,您可以給

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/'); 

很抱歉,如果這是一個非常簡單的問題,並感謝!

+0

這是一大堆$。 – Krinkle

回答

5

問題是Node.JS的HTTP請求模塊沒有遵循您給出的重定向。

看到這個問題更多:How do you follow an HTTP Redirect in Node.js?

基本上,你可以通過標題,看起來和處理重定向自己,或使用該模塊的少數之一。我已經使用了「請求」庫,並且自己也有好運氣。 https://github.com/mikeal/request

+0

非常感謝您的快速回復!我會看看。 – dxu

+0

上面的$是怎麼回事,那些是有意的? – jcolebrand

+0

@jcolebrand,他們看起來像是從他的編輯中複製粘貼的。它們似乎是指示行尾字符。 – Brad

3
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})) 
} 
+0

那麼爲什麼'res.statusCode == 301'在這一點上,在哪裏是'我',你用什麼混淆器來生成此代碼:P –

+0

哦,'f'是遞歸的; àlaline〜12 ... LOL –

+0

增加了i功能,當然不會讓它變得更加困惑....至於混音器哇,我不記得寫這個我一定是被擁有。 – Prospero

1

這個問題現在是老了,但我得到了同樣的錯誤301和這些答案實際上並沒有幫我解決這個問題。

我寫了相同的代碼:

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(); 

所以一段時間後,我意識到,有一個在這個代碼真的很小的錯誤,是主機部分:

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

你必須添加「WWW 「。在你的URL之前獲取html內容,否則會出現301錯誤。

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

301響應不是一個錯誤,它是一個永久的重定向和這個人的問題的核心基礎。該網站被重定向爲http 301響應,並且node.js http不遵循重定向,您必須爲此編寫代碼或使用將爲您執行此操作的衆多模塊之一。 – Brettski