node.js - node js请求获取页面错误500

标签 node.js get http-headers request

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

最佳答案

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

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

关于node.js - node js请求获取页面错误500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683240/

相关文章:

hadoop fs - 在 ubuntu 中无法工作

linux - 带 GET 参数的 wget?

perl - 如何更改 Perl 的 LWP 发送的 HTTP 请求 header 的顺序?

javascript - 模板中的模板 Handlebars

node.js - 有没有办法使用内联编辑器而不是使用后续意图来要求用户确认?

java - 将枚举值列表作为 HTTP 查询参数传递

asp.net - 为什么当我调用 Response.Redirect() 时会得到 "Cannot redirect after HTTP headers have been sent"?

jquery - 带有自定义 HTTP header 字段的 JSON 发布

node.js - 将 Node 生产部署到 AWS 的当前最佳实践是什么

javascript - 使用 node-addon-api 将 C 库回调传递给 NodeJS EventEmitter