nodeJS:抛出新的错误('在发送之后不能设置标题。');

时间:2013-11-28 07:39:31

标签: node.js express

这是常见的问题,但没有得到解决方案。所以我再次把它放在这里。

这是 app.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
require('./routes/sockets.js').initialize(server);

路径文件夹

中的 sockets.js
var io = require('socket.io');
exports.initialize = function(server) {console.log('init called');
    io = io.listen(server);console.log('io');
    io.sockets.on("connection", function(socket){console.log("connected");
        socket.send(JSON.stringify({type:'serverMessage',message: 'Welcome to the most interesting chat room on earth!'}));
        socket.on('message', function(message){
            message= JSON.parse(message);
            if(message.type == "userMessage")
            {
                socket.broadcast.send(JSON.stringify(message));
                message.type = "myMessage";
                socket.send(JSON.stringify(message));
            }
        });
    });
};

内部sockets.js console.log('init called'); console.log('io');打印得很好。当我使用npm start运行此应用程序时出现以下错误:

npm WARN package.json application-name@0.0.1 No repository field.
npm WARN package.json application-name@0.0.1 No readme data.

> application-name@0.0.1 start D:\Applications\New folder\node\chat
> node app.js

init called
   info  - socket.io started
io
Express server listening on port 3000
GET / 200 312ms - 511

http.js:707
    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.js:707:11)
    at ServerResponse.res.setHeader (D:\Applications\New folder\node\chat\node_m
odules\express\node_modules\connect\lib\patch.js:59:22)
    at next (D:\Applications\New folder\node\chat\node_modules\express\node_modu
les\connect\lib\proto.js:153:13)
    at Function.app.handle (D:\Applications\New folder\node\chat\node_modules\ex
press\node_modules\connect\lib\proto.js:198:3)
    at Server.app (D:\Applications\New folder\node\chat\node_modules\express\nod
e_modules\connect\lib\connect.js:66:31)
    at Manager.handleRequest (D:\Applications\New folder\node\chat\node_modules\
socket.io\lib\manager.js:564:28)
    at Server.<anonymous> (D:\Applications\New folder\node\chat\node_modules\soc
ket.io\lib\manager.js:118:10)
    at Server.EventEmitter.emit (events.js:117:20)
    at HTTPParser.parser.onIncoming (http.js:2051:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23
)

当我在浏览器中访问localhost:3000时,一切都会发生。我正在使用节点版本v0.10.10,表达3.4.4

1 个答案:

答案 0 :(得分:1)

得到了解决方案!!这是因为某些版本不匹配。这是我使用的依赖

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.4",
    "socket.io": "0.9",
    "jade": "*"
  }
}