这个是Server,客户端会用Ajax请求这个地方 // Nodejs部分,主要作用是接收前端关键词,抓取百度知道页面。返回页面给前端 var http = require(‘http’); var cheerio = require(‘cheerio’); var iconv = require(‘iconv-lite’); http.createServer(function (req, res) { res.writeHead(200, { ‘Content-Type’: ‘text/html’, ‘Access-Control-Allow-Origin’ : ‘*’ }); var postData = “”; var ret = 0; req.setEncoding(‘utf-8’); req.addListener(‘data’, function(chunk){ postData += chunk; }); req.addListener(‘end’, function(){ var value = postData.replace(/kw=/, ‘’), result = 0; doRequest(value, 0); }); res.end(’{“rank”:’+result+’}’); }).listen(10088);
这个是当server端得到数据后,会请求这个doRequest方法,这里面会去抓百度的页面,计算出关键词排的在第几返回出来。 var doRequest = function(kw, page){ page = page * 10; var options = { host: ‘zhidao.baidu.com’, port: 80, path: ‘/search?word=’+kw+’&pn=’+page }; http.get(options, function(res) { var buffers = [], size = 0; res.on(‘data’, function(buffer) { buffers.push(buffer); size += buffer.length; }); res.on(‘end’, function() { var buffer = new Buffer(size), pos = 0; for(var i = 0, l = buffers.length; i < l; i++) { buffers[i].copy(buffer, pos); pos += buffers[i].length; } var gbk_buffer = iconv.decode(buffer,‘GBK’); $ = cheerio.load(gbk_buffer.toString()); // 获取页面前三个的优质回答 var target = “DARRY RING”; var isBreak = false; var htmlTop = $("#cluster-items").find(".con-all").slice(0, 3); htmlTop.each(function(){ var tContent = $(this).text().replace(/\s+/g, “”); tContent = tContent.toLowerCase(); if (tContent.indexOf(“darryring”) > 0 ){ // 当找到DY的时候,退出循环 isBreak = true; return false; } }); if (isBreak == true){ return {keyword : kw, score : 1}; } var html = $("#wgt-list").find(“dd.answer”); var n = 0; html.each(function(i, elem){ var content = $(this).text().replace(/\s+/g, “”); content = content.toLowerCase(); if (content.indexOf(“darryring”) > 0 && n <= html.length ){ n = i + 1; return false; } }); if(n == 0){ page++; if (page < 5){ doRequest(kw, page); }else{ return {keyword : kw, score : 9999}; } }else{ var num = page + n; return {keyword : kw, score : num}; } }); res.on(‘error’, function(e){ console.log("Got error: " + e.message); }) }) }
现在就是调试的时候,doRequest里面都成功了,但是不返回到客户端。求指正啊,我要怎么改?以前没写过异步