2013-12-19 73 views
3

我想要使用節點js中的請求模塊向頁面發出請求,但是這有一個錯誤500。 這是JSON響應節點js請求獲取頁面錯誤500

{ 
    "readable": false, 
    "domain": null, 
    "_maxListeners": 10, 
    "httpVersion": "1.1", 
    "complete": true, 
    "_pendingIndex": 0, 
    "url": "", 
    "method": null, 
    "statusCode": 500, 
    "_consuming": true, 
    "_dumped": false, 
    "httpVersionMajor": 1, 
    "httpVersionMinor": 1, 
    "upgrade": false, 
    "body": "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>500 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested\n    site <a href=\"http:// HTTP/1.1/\"> HTTP/1.1</a> is not presently available on\n    this <a href=\"http://www.hybrid-cluster.com/\">web cluster</a>.\n    This may be a temporary issue, so please try again in a few\n    moments.</p>\n\n</body></html>\n", 
    "_readableState": { 
    "highWaterMark": 16384, 
    "length": 0, 
    "pipes": null, 
    "pipesCount": 0, 
    "flowing": false, 
    "ended": true, 
    "endEmitted": true, 
    "reading": false, 
    "calledRead": true, 
    "sync": false, 
    "needReadable": true, 
    "emittedReadable": false, 
    "readableListening": false, 
    "objectMode": false, 
    "defaultEncoding": "utf8", 
    "ranOut": false, 
    "awaitDrain": 0, 
    "readingMore": false, 
    "decoder": null, 
    "encoding": null, 
    "buffer": [] 
    }, 

這是我使用的要求

app.get('/', function(req, res){ 
    var options = { 
     url: 'http://www.metallicabyrequest.com/' 
    } 
    request(options, function (error, response, html) { 
     /*if (!error && response.statusCode == 200) { 
      res.json(html); 
     }*/ 
     if(error){ 
      res.json(error); 
     }else{ 
      res.json(response) 
     } 
    }); 
}); 

有沒有什麼辦法讓防止錯誤500代碼的一部分嗎?我讀的是如果你改變了頭,可能是該請求的作品,但我不知道該怎麼辦......

回答

4

該網站需要「主機」的請求頭:

var options = { 
    url: 'http://www.metallicabyrequest.com/', 
    headers: { 
     'Host': 'www.metallicabyrequest.com' 
    } 
} 
+0

謝很多,效果很好! –