Mar*_*uza 0 squarespace node.js
http.get
当我对转到 SquareSpace (SS) 站点的 URL进行简单操作时,我收到一条 403 消息。我知道该网站正在运行并且服务器可以访问它。这是针对 SS 站点的一个简单示例(不是我的,但会产生相同的问题):
显示服务器可以访问站点:
curl http://www.letsmoveschools.org
This returns all the HTML from the site...
节点应用程序
var http = require('http');
var url;
url = 'http://www.letsmoveschools.org/';
var req = http.get(url, function(res) {
res.on('data', function(chunk) {
//Handle chunk data
});
res.on('end', function() {
// parse xml
console.log(res.statusCode);
});
// or you can pipe the data to a parser
//res.pipe(dest);
});
req.on('error', function(err) {
// debug error
console.log('error');
});
Run Code Online (Sandbox Code Playgroud)当我现在运行节点应用程序时,node app.js
它会输出403
状态代码。
我已经在其他网站上尝试过此代码,它工作正常,只是不适用于 squarespace 网站。知道 SS 上的配置还是我需要在 Node 中做的其他事情吗?
问题是远程服务器期望/需要User-Agent
标头,而节点不会自动发送此类标头。添加该内容,您应该会收到 200 响应:
// ...
url = 'http://www.letsmoveschools.org/';
var opts = require('url').parse(url);
opts.headers = {
'User-Agent': 'javascript'
};
var req = http.get(opts, function(res) {
// ...
Run Code Online (Sandbox Code Playgroud)