错误:发送后无法设置标头。 - Nodejs ExpressJs

时间:2017-04-06 11:53:44

标签: javascript node.js express

我从其他一些资源获得了下面的代码,试图使用Twitter api,但它给我一些错误。

我无法理解如何解决这个问题。有时,当我两次使用回调时会出现错误,但似乎没有。

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:'' });
};

我得到的错误:

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)

非常感谢您的帮助!

0 个答案:

没有答案