【问题标题】:Node js request get page error 500Node js请求获取页面错误500
【发布时间】:2013-12-19 13:38:59
【问题描述】:

我想使用 node 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?我读到如果您更改标头,可能是请求有效,但我不知道该怎么做...

【问题讨论】:

    标签: node.js get http-headers request


    【解决方案1】:

    该站点在请求标头中需要“主机”:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 2020-08-20
      • 2020-11-29
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多