发送标头后无法设置标头 [英] Can't set headers after they are sent

查看:99
本文介绍了发送标头后无法设置标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用节点版本0.10.3和Express 3.1.1.

I'm using node version 0.10.3 and Express 3.1.1.

我试图返回一个简单的响应,但出现以下错误:

I'm trying to return a simple response, but I'm getting the following error:

http.js:692
    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:692:11)

我已经阅读了很多,并尝试了许多解决方案,但是我仍然遇到该错误. 我使用了node-inspector,在该行之后出现了错误-

I've read a lot about it, and tried many solutions but I'm still getting that error. I've used node-inspector and i'm getting the error after that line -

res.render('index', { title: 'Express' });

app.js:

/**
 * Module dependencies.
 */

var express = require('express')
  , http = require('http')
  , path = require('path')
  , socketio = require('socket.io');

var app = express()
  , server = http.createServer(app)
  , io = socketio.listen(server);

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'hjs');
  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'));
});


io.configure('production', function(){
  io.enable('browser client etag');
  io.set('log level', 1);
});

io.configure('development', function(){
  io.set('log level', 1);
});

io.sockets.on('connection', function(socket) {
    socket.on('event', function(event) {
        socket.join(event);
    });
});

require('./routes')(app, io);

routes/index.js:

var utils = require('../utils')
  , config = require('../config')
  , io;

module.exports = function(app, socketio) {
  io = socketio;
  app.get('/', index);
};

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

views/index.hjs:

<!DOCTYPE html>
<html>
  <head>
    <title>{{ title }}</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>{{ title }}</h1>
    <p>Welcome to {{ title }}</p>
  </body>
</html>

package.json:

{
  "name": "blabla",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "app.js"
  },
  "dependencies": {
    "express": "3.0.3",
    "hjs": "0.0.4",
    "cradle": "0.6.4",
    "twiliosig": "0.0.1",
    "socket.io": "0.9.11"
  },
  "subdomain": "blabla",
  "engines": {
    "node": "0.6.x"
  }
}

有线索吗?

推荐答案

您的socket.io版本似乎出错.

Seems to be an error with your version of socket.io.

使用此:

"dependencies": {
  "express": "3.0.x",
  "hjs": "0.0.4",
  "cradle": "0.6.4",
  "twiliosig": "0.0.1",
  "socket.io": "0.9.x"
}

已通过socket.io@0.9.16测试

Tested with socket.io@0.9.16

这篇关于发送标头后无法设置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆