I wrote some server side code that should get live data from yahoo and then print it to the console and to the browser when i'm running the server, the problem is that i can't find a function that is printing to the document from the request block. This is my code:
我写了一些服务器端代码,应该从雅虎获取实时数据,然后在我运行服务器时将其打印到控制台和浏览器,问题是我找不到从中打印到文档的功能请求块。这是我的代码:
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
var util = require('util');
tempvar = null;
var server = http.createServer(function(req, res) {
//writing the headers of our response
res.writeHead(200, {
'Content-Type': 'text/plain'
});
// Variable Deceleration
// TODO: move from the global scope
var ticker = "IBM";
var yUrl = "http://finance.yahoo.com/q/ks?s=" + ticker;
var keyStr = new Array();
testTemp = null;
//
// The main call to fetch the data, parse it and work on it.
//
request(yUrl, function(error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
// the keys - We get them from a certain class attribute
var span = $('.time_rtq_ticker>span');
stockValue = $(span).text();
console.log("Stock - " + ticker + " --> text " + stockValue);
//res.write("Stock - " + ticker + " --> text " + stockValue);
testTemp = stockValue;
//
-- end of request --
res.write("Stock value for: " + ticker + " is --> " + testTemp + "\n");
res.write("12333\n");
res.write('something\n');
//printing out back to the client the last line
res.end('end of demo');
## Heading ##
}
});
});
server.listen(1400, '127.0.0.1');
This is the error that i got in the console Files\node.js\node_modules\YfTemp.js:49 >>; SyntaxError: Unexpected end of input at Module._compile at Object.Module._extensions..js at Module.load at Function.Modul._load at Function.Module.runMain at strartup at node.js:906:3
这是我在控制台Files \ node.js \ node_modules \ YfTemp.js中得到的错误:49 >>; SyntaxError:在Module.load处的Module._compile处的Object.Module._extensions..js处的意外结束输入位于Function.Modul._load的Function.Modul._load位于node.js处的strartup处的函数:906:3
1 个解决方案
#1
3
Request
works asynchronously. You need to put the printing part of your script inside the request callback block. Otherwise, ticker
is not defined yet when the printing lines are reached.
请求异步工作。您需要将脚本的打印部分放在请求回调块中。否则,到达打印行时尚未定义自动收报机。
request(yUrl, function(error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
// the keys - We get them from a certain class attribute
var span = $('.time_rtq_ticker>span');
stockValue = $(span).text();
console.log("Stock - " + ticker + " --> text " + stockValue);
//res.write("Stock - " + ticker + " --> text " + stockValue);
testTemp = stockValue;
// -- end of request --
res.write("Stock value for: " + ticker + " is --> " + testTemp + "\n");
res.write("12333\n");
res.write('something\n');
//printing out back to the client the last line
res.end('end of demo');
}
});
#1
3
Request
works asynchronously. You need to put the printing part of your script inside the request callback block. Otherwise, ticker
is not defined yet when the printing lines are reached.
请求异步工作。您需要将脚本的打印部分放在请求回调块中。否则,到达打印行时尚未定义自动收报机。
request(yUrl, function(error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
// the keys - We get them from a certain class attribute
var span = $('.time_rtq_ticker>span');
stockValue = $(span).text();
console.log("Stock - " + ticker + " --> text " + stockValue);
//res.write("Stock - " + ticker + " --> text " + stockValue);
testTemp = stockValue;
// -- end of request --
res.write("Stock value for: " + ticker + " is --> " + testTemp + "\n");
res.write("12333\n");
res.write('something\n');
//printing out back to the client the last line
res.end('end of demo');
}
});