服务器请求数据的时候第一次有数据,再刷新提示retry
Posted 柒夏°
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了服务器请求数据的时候第一次有数据,再刷新提示retry相关的知识,希望对你有一定的参考价值。
var http = require("http"); var url = require("url"); var qs = require("querystring"); http.createServer(function (req, res) { //设置请求头 res.setHeader("Access-Control-Allow-Origin" , "*"); //转化 url 为 对象形式 var postObj = url.parse(req.url).query; var query = qs.parse(postObj); // console.log(query); var result = ""; //query.myUrl 就是 要请求的地址 // http.get(query.myUrl , function (request) { // request.on("data" , function (data) { // result += data; // }); // request.on("end" , function () { // res.end(result); // }) // }).on("error" , function (err) { // console.log(err); // }) APIGET(query.myUrl , function (a , b) { // console.log(a); // console.log(b); // // console.log("收到数据"); // console.log(b); var xxx = JSON.parse(b); // console.log(xxx.message); if(xxx.message == "retry"){ } else { res.end(b); } }) }).listen("3000" , function () { console.log("正在监听3000.....") }); var APIGET = function (url, callback) { //下面是发送请求时的延时处理,一般用不到 var requestTimer = setTimeout(function () { req.abort(); debug(‘......Request Timeout......‘); },5000); // http://iu.snssdk.com/neihan/stream/mix/v1/?mpic=1&webp=1&essence=1&content_type=-104&message_cursor=85926841&count=30&min_time=1494403102&screen_width=1080&double_col_mode=0&local_request_tag=1494403105852&iid=9839985348&device_id=36086164659&ac=wifi&channel=tengxun&aid=7&app_name=joke_essay&version_code=618&version_name=6.1.8&device_platform=android&ssmix=a&device_type=vivo+X7&device_brand=vivo&os_api=22&os_version=5.1.1&uuid=862341038726597&openudid=e389f521b9527a18&manifest_version_code=618&resolution=1080*1920&dpi=480&update_version_code=6184%20HTTP/1.1%20Cache-Control:%20max-stale=0%20Host:%20iu.snssdk.com%20Connection:%20Keep-Alive%20Cookie:%20uuid=862341038726597;%20login_flag=4938b92d1e4429d3cb43d7ad16a0e657;%20sessionid=77eb9ef8c2025b98153ed683e0575d4a;%20sid_tt=77eb9ef8c2025b98153ed683e0575d4a;%20sid_guard=%2277eb9ef8c2025b98153ed683e0575d4a|1493624834|2592000|Wed\054%2031-May-2017%2007:47:14%20GMT%22;%20appVersion=6.1.8;%20_ga=GA1.2.208402412.1494331614;%20_gid=GA1.2.967802619.1494331616;%20qh[360]=1;%20install_id=9839985348;%20ttreq=1$75d520312b3c0cf64f7714d71b33a422fb5c2743;%20alert_coverage=54%20User-Agent:%20okhttp/2.7.5%20Paros/3.2.13"; var op = { host:"iu.snssdk.com", method:‘GET‘, path:url }; var req = http.request( op,function(res) { clearTimeout(requestTimer); //下面是请求接口数据,得不到回应时,我们关闭等待返回数据的状态,因为有5秒的定时器, //5秒内如果收到了完整的数据,http模块会自动跳转到res.on(‘end‘, function(){}) //因为我们在res.on(‘end‘, function(){})的回调函数中clearTimeout(responseTimer), //清除了这个定时器,所以就不用担心在接受到数据后定时器还反复执行。 var responseTimer = setTimeout(function () { res.destroy(); debug(‘......Response Timeout......‘); },5000); var recvData = ""; res.on(‘data‘, function(chunk) { recvData += chunk; // debug(recvData); }); res.on(‘end‘, function() { clearTimeout(responseTimer); if (callback) { callback(null, recvData); } // debug("请求结束"); }); }); req.on(‘error‘, function (e) { if (callback) { callback(e, null); } }); req.end(); };