我正在使用 nettutplus 介绍系列学习 node.js。在本系列中,有一个使用 twitter 流 api 创建 node.js 应用程序的课程,视频中显示的代码与我编写的代码完全相同,但是它不起作用。
这是我的代码
var https = require("https");
var options = {
host: 'stream.twitter.com',
path: '/1/statuses/filter.json?track=bieber',
method: 'GET',
headers: {
"Authorization": "Basic " + new Buffer("username:password").toString("base64")
}
};
var request = https.request(options, function(response){
var body = '';
response.on("data", function(chunk){
var tweet = JSON.parse(chunk);
console.log("Tweet: "+ tweet.text);
});
response.on("end", function(){
console.log("Disconnected");
});
});
request.end();
错误是得到是这个
未定义:1
^
语法错误:意外的令牌 <
at Object.parse (native)
at IncomingMessage.<anonymous> (C:\Users\monk\Desktop\folder\node\twitter.js:14:20)
at IncomingMessage.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (_stream_readable.js:736:14)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at IncomingMessage.Readable.push (_stream_readable.js:127:10)
at HTTPParser.parserOnBody [as onBody] (http.js:141:22)
我正在使用windows和cmd。