节点js请求获取页面错误500

时间:2013-12-19 13:38:59

标签: node.js get http-headers request

我想使用节点js中的请求模块向页面发出请求,但这有错误500。 这是json响应的一部分

{
  "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?我读到如果你改变标题,可能是请求有效,但我不知道该怎么做......

1 个答案:

答案 0 :(得分:4)

该网站在请求标题中需要“主机”:

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