I'm trying to use node-twitter module in express framework. I'm pretty new to node, I understand the basic routing, rendering jade templates and installing packages and etc. But I'm very happy to see so many packages out there, but when I get in to the details I don't really understand how to use them in express framework.
Currently, I'm using node-twitter module (https://github.com/desmondmorris/node-twitter) and would like to use Streaming API. Below are the code snippets which I would need to get started with streaming API. I've got all the required keys and stuck when it comes to express. So; my question is, how to pass this to a route?
var util = require('util'),
twitter = require('twitter');
var twit = new twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
twit.stream('user', {track:'nodejs'}, function(stream) {
stream.on('data', function(data) {
console.log(util.inspect(data));
});
// Disconnect stream after five seconds
setTimeout(stream.destroy, 5000);
});
As far as I know, there are two ways of routing, one is rendering a template and other one is directly sending a response.
app.get('/',function(req, res){
res.render ('index');
});
app.get('/test',function(req,res){
res.send('this is a test');
})
Now, how set up routes for twitter streaming api? please advice.
I think you are attempting to fit a square peg into a round hole.
The twitter streaming module streams data synchronously from twitter while route requests are initiated by a user making an http request to your site.
To provide the http request serviced by a route some of the tweets that have come in via the twitter streaming module you could push tweets gathered from the stream.on('data' section into an array you initialize at the same level as the route handler.
Another approach would be to use an asynchronous communication channel such as socket.io and pass tweets along to connected users as they come in.