节点.js 返回 301 重定向的请求
Node.js Requests returning 301 redirects
我是node.js的新手,但我想尝试一些基本代码并提出一些请求。目前,我正在使用 OCW 搜索 (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');
自
require('https');
问题是 Node.JS 的 HTTP 请求模块没有遵循您给出的重定向。
有关更多信息,请参阅此问题:如何在 Node.js 中遵循 HTTP 重定向?
基本上,您可以浏览标头并自己处理重定向,也可以为此使用少数模块之一。 我使用过"请求"库,并且自己运气很好。 https://github.com/mikeal/request
var http = require('http');
var find_link = function(link, callback){
var root ='';
var f = function(link){
http.get(link, function(res) {
if (res.statusCode == 301) {
f(res.headers.location);
} else {
callback(link);
}
});
}
f(link, function(t){i(t,'*')});
}
find_link('http://somelink.com/mJLsASAK',function(link){
console.log(link);
});
function i(data){
console.log( require('util').inspect(data,{depth:null,colors:true}) )
}
这个问题现在已经很旧了,但我得到了同样的 301 错误,这些答案实际上并没有帮助我解决问题。
我写了同样的代码:
var options = {
hostname: 'google.com',
port: 80,
path: '/',
method: 'GET',
headers: {
'Content-Type': 'text/plain',
}
};
var http = require('http');
var req = http.request(options, function(res) {
console.log('STATUS:',res.statusCode);
console.log('HEADERS: ', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log(chunk);
});
res.on('end', function() {
console.log('No more data in response.');
});
});
req.on('error', function(e) {
console.log('problem with request: ', e.message);
});
console.log(req);
req.end();
所以过了一段时间,我意识到这段代码中有一个非常小的错误,即主机名部分:
var options = {
hostname: 'google.com',
...
您必须在URL之前添加"www."才能获取HTML内容,否则会出现301错误。
var options = {
hostname: 'www.google.com',
相关文章:
- 单击事件中的ajax请求后重定向
- 为什么这个请求不会遵循我的龙卷风重定向
- 如何在fetch post调用后管理重定向请求
- Hapi-js重定向到另一个带有额外请求数据的路由
- Laravel:在 ajax 请求的会话过期后重定向到登录
- 由于重定向URI无效,Key斗篷令牌请求被拒绝
- 使用Javascript重定向并设置请求属性
- 如何通过 AJAX 调用将请求重定向到指定的 PHP 页面
- FB应用程序请求重定向到画布页面而不是标签页
- 如何在 Nginx 中将所有 Angular 请求重定向到 index.html
- 将所有http请求重定向到帆js中的https
- 有没有办法更改浏览器's的URL,而不将第一个原始请求重定向到服务器
- 如何从ajax请求重定向响应
- 在服务器上使用curl -如何将javascript请求重定向到目标服务器
- 寻找Facebook请求重定向
- Ajax请求重定向到index.html
- 如何将所有地址栏请求重定向到一个索引页?并使用请求的子文件夹作为输入
- 将nodejsexpress静态请求重定向到https
- ASP.NET MVC 3 将请求重定向到根页面
- 将所有请求重定向到index.php并在页面上显示URL