所以在这个(简化的)代码中,当有人点击我的节点服务器时,我向另一个网站发出GET请求并将HTML页面标题打印到控制台.工作良好:
var http = require("http");
var cheerio = require('cheerio');
var port = 8081;
s = http.createServer(function (req, res) {
var opts = {
method: 'GET',
port: 80,
hostname: "pwoing.com",
path: "/"
};
http.request(opts, function(response) {
console.log("Content-length: ", response.headers['content-length']);
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function() {
dom = cheerio.load(str);
var title = dom('title');
console.log("PAGE TITLE: ",title.html());
});
}).end();
res.end("Done.");
}).listen(port, '127.0.0.1');
Run Code Online (Sandbox Code Playgroud)
但是,在实际应用中,用户可以指定要点击的URL.这意味着我的节点服务器可能正在下载20GB的电影文件或其他什么.不好.内容长度标头不能用于停止它,因为它不是由所有服务器传输的.那么问题是:
如果收到前10KB,我怎么能告诉它停止GET请求?
干杯!
尝试在Chrome的JS控制台中输入此内容.我发现这是一个正则表达式,用于检查某些内容是否为有效的URL:
"http://www.kvraudio.com/".match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);
Run Code Online (Sandbox Code Playgroud)
返回匹配,应该是.现在试试这个:
"tp:/www.kvraudio.com/forum/viewtopic.php".match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);
Run Code Online (Sandbox Code Playgroud)
返回Null,因为它不匹配.现在.....试试这个:
"http://www.kvraudio.com/forum/viewtopic.php?p=5238905".match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);
Run Code Online (Sandbox Code Playgroud)
没有!JS似乎已经死了或以某种方式陷入循环.如果我在实际网页中使用上述内容,则会停止响应.甚至不会滚动!有人对此有任何解释吗?我做错了什么?!
我需要能够将图像和一些表单字段从客户端canvas元素发送到PHP脚本,最后是$ _POST和$ _FILES.当我这样发送时:
<script type="text/javascript">
var img = canvas.toDataURL("image/png");
...
ajax.setRequestHeader('Content-Type', "multipart/form-data; boundary=" + boundary_str);
var request_body = boundary + '\n'
+ 'Content-Disposition: form-data; name="formfield"' + '\n'
+ '\n'
+ formfield + '\n'
+ '\n'
+ boundary + '\n'
+ 'Content-Disposition: form-data; name="async-upload"; filename="'
+ "ajax_test64_2.png" + '"' + '\n'
+ 'Content-Type: image/png' + '\n'
+ '\n'
+ img
+ '\n'
+ boundary;
ajax.send(request_body);
</script>
Run Code Online (Sandbox Code Playgroud)
$ _POST和$ _FILES都回填了,但$ _FILES中的图像数据仍然需要解码如下:
$loc = $_FILES['async-upload']['tmp_name'];
$file = fopen($loc, 'rb');
$contents = fread($file, filesize($loc)); …Run Code Online (Sandbox Code Playgroud)