2013-12-19 52 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

谢很多,效果很好! –