我必须使用node.js构建一个Twitter可视化应用程序,我正在按照教程http://blog.safe.com/2014/03/twitter-stream-api-map/
我按照步骤操作,现在,当我跑步时
node server.js
在存储server.js的目录之后,没有任何反应。
在localhost:8081
,该计划无效。
目录结构是
twitter --> node_modules, public, server.js
我使用npm来安装所需的包 并永远用于运行服务器。
我已经尝试了一些我在网上找到的修复方法,坚果没什么用。
非常感谢你的帮助。
这是server.js:
//Setup web server and socket
var twitter = require('twitter'),
express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
//Setup twitter stream api
var twit = new twitter({
consumer_key: 'XXX',
consumer_secret: 'XXX',
access_token_key: 'XXX',
access_token_secret: 'XXX'
}),
stream = null;
//Use the default port (for beanstalk) or default to 8081 locally
server.listen(process.env.PORT || 8081);
//Setup rotuing for app
app.use(express.static(__dir + '/public'));
//Create web sockets connection.
io.sockets.on('connection', function (socket) {
socket.on("start tweets", function() {
if(stream === null) {
//Connect to twitter stream passing in filter for entire world.
twit.stream('statuses/filter', {'locations':'-180,-90,180,90'}, function(stream) {
stream.on('data', function(data) {
// Does the JSON result have coordinates
if (data.coordinates){
if (data.coordinates !== null){
//If so then build up some nice json and send out to web sockets
var outputPoint = {"lat": data.coordinates.coordinates[0],"lng": data.coordinates.coordinates[1]};
socket.broadcast.emit("twitter-stream", outputPoint);
//Send out to web sockets channel.
socket.emit('twitter-stream', outputPoint);
}
else if(data.place){
if(data.place.bounding_box === 'Polygon'){
// Calculate the center of the bounding box for the tweet
var coord, _i, _len;
var centerLat = 0;
var centerLng = 0;
for (_i = 0, _len = coords.length; _i < _len; _i++) {
coord = coords[_i];
centerLat += coord[0];
centerLng += coord[1];
}
centerLat = centerLat / coords.length;
centerLng = centerLng / coords.length;
// Build json object and broadcast it
var outputPoint = {"lat": centerLat,"lng": centerLng};
socket.broadcast.emit("twitter-stream", outputPoint);
}
}
}
stream.on('limit', function(limitMessage) {
return console.log(limitMessage);
});
stream.on('warning', function(warning) {
return console.log(warning);
});
stream.on('disconnect', function(disconnectMessage) {
return console.log(disconnectMessage);
});
});
});
}
});
// Emits signal to the client telling them that the
// they are connected and can start receiving Tweets
socket.emit("connected");
});
创建Twitter应用后,我已经填好了XXX个字段。