Node.js 请求返回 301 重定向

2024-02-14

我是 Node.js 的新手,但我想尝试一些基本代码并提出一些请求。目前,我正在玩 OCW 搜索(http://www.ocwsearch.com/ http://www.ocwsearch.com/),我正在尝试使用他们的示例搜索请求提出一些基本请求:

但是,无论我尝试提出什么请求(即使我只是查询 google.com),它都会返回我

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>

我不太确定发生了什么事。我查过 nginx,但大多数关于它的问题似乎都是由正在设置自己的服务器的人提出的。我尝试使用 https 请求,但返回错误“ENOTFOUND”。

我的代码如下:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');

    var options = {
      host:'ocwsearch.com',
      path:
      '/api/v1/search.json?q=statistics&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',
      method: 'GET'
    }  


    var req = http.request(options, function(res) {
      console.log("statusCode: ", res.statusCode);
      console.log("headers: ", res.headers);
      res.on('data', function(d) {
            process.stdout.write(d);
      });
    });
    req.end();

    req.on('error', function(e) {
      console.error(e);
    });


}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

抱歉,如果这是一个非常简单的问题,感谢您提供的任何帮助!


对我来说,我尝试获取的网站将我重定向到安全协议。所以我改变了

require('http');

to

require('https');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Node.js 请求返回 301 重定向 的相关文章

随机推荐