0

I got the code below from some other resources, trying to work with Twitter api, but it throws me some error.

I couldn't understand how to resolve that. Sometimes the error will appear when I use callbacks twice, but nothing seems like that.

var express = require('express'), 
    app     = express(), 
    server  = require('http').createServer(app), 
    routes  = require('./routes'), 
    io      = require('socket.io').listen(server), 
    path    = require('path'), 
    twitter = require('ntwitter');


app.configure(function()
  {   
    app.set('port', process.env.PORT || 5000); 
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.favicon());
    app.use(express.logger('dev'));   
    app.use(express.bodyParser());   
    app.use(express.methodOverride());   
    app.use(app.router);   
    app.use(express.static(path.join(__dirname, 'public'))); });

app.configure('development', function(){   
  app.use(express.errorHandler()); 
});

server.listen(app.get('port'), function(){   
  console.log("Express server listening on port " + app.get('port')); 
}); 
app.get('/', routes.index); var twit = new twitter({ 

twit.stream('statuses/filter', { locations: criteria }, function(stream) { 
    stream.on('data', function (data) {
        var geo=false,latitude,longitude;

        io.sockets.volatile.emit('tweets', {

        });
    });
});

index.js

exports.index = function(req, res){
  res.render('index', { title: '', criteria:'' });
};

Errors I am getting:

Express server listening on port 5000
GET / 200 30ms - 8.43kb
_http_outgoing.js:335
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
    at ServerResponse.res.setHeader (/home/jayasurya/Desktop/node-twitter-modules/TweetMapViewer/node_modules/connect/lib/patch.js:59:22)
    at next (/home/jayasurya/Desktop/node-twitter-modules/TweetMapViewer/node_modules/connect/lib/proto.js:153:13)
    at Function.app.handle (/home/jayasurya/Desktop/node-twitter-modules/TweetMapViewer/node_modules/connect/lib/proto.js:198:3)
    at Server.app (/home/jayasurya/Desktop/node-twitter-modules/TweetMapViewer/node_modules/connect/lib/connect.js:66:31)
    at Manager.handleRequest (/home/jayasurya/Desktop/node-twitter-modules/TweetMapViewer/node_modules/socket.io/lib/manager.js:564:28)
    at Server.<anonymous> (/home/jayasurya/Desktop/node-twitter-modules/TweetMapViewer/node_modules/socket.io/lib/manager.js:118:10)
    at emitTwo (events.js:92:20)
    at Server.emit (events.js:172:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:528:12)

Many thanks for any help!

c0bra
  • 2,982
  • 23
  • 34
my_first_step
  • 33
  • 1
  • 8
  • something's wrong with routes.index – hisener Apr 06 '17 at 11:58
  • This is caused by your routes, you are probably calling `next() `when there is no middleware to handle the next request. You can simply close the stream at this point by calling `res.end()` or `return false` in your `routes.index` – theterminalguy Apr 06 '17 at 12:08
  • @DamianSimonPeter exports.index = function(req, res){ res.render('index', { title: 'asd asd asd', criteria:'sda asdf' }); res.end(); }; just change it as you said, but throws same error – my_first_step Apr 06 '17 at 12:15
  • @my_first_step please make ```server.listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });``` The last thing in your file. – theterminalguy Apr 06 '17 at 12:25
  • @DamianSimonPeter am not getting what are you saying.. can you pls make it clearly..? just did compare your code with mine.but seems there is no difference – my_first_step Apr 06 '17 at 12:39
  • can you pls check again my codes. did some editing – my_first_step Apr 06 '17 at 13:03
  • What @DamianSimonPeter was trying to say, is to put `server.listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); });` **after** all the rest of your code, at the bottom in your app. – Xogno Apr 06 '17 at 13:10

0 Answers0