node.js - Nodejs : Connect and "writeHead" in first connect-callback

标签 node.js node.js-connect

在 connect 的第一个回调中调用“res.writeHead(...)”时,我总是收到错误“错误:发送后无法设置 header 。”:

var http = require('http');
var connect = require('connect');

var simpleApp = connect();

simpleApp
    .use(function(req, res, next){

        res.writeHead(200, { 'content-type': 'text/html' });
        res.write('response powered by SIMPLE connect-object as middelware');

        console.log('Pre');
        next();
        console.log('Post');
    })
    .use(function(req, res, next){
        console.log('I am the header guy!');
        next();
    })
    .use('/admin', function(req, res, next){
        console.log('someone entered the admin area....');
        next();
    })
    .use(function(req, res){
        console.log('reached the tail of the "chain of responsibility!!!');
        res.end();
    });

http.createServer(simpleApp).listen(process.env.PORT || 3000);

console.log('Running on port "' + (process.env.PORT || 3000) + '"');

// just a save-guard to stop the process after some time...
setTimeout(function(){
    process.exit(0);
}, 20000);

这是错误消息:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
    at ServerResponse.res.setHeader (C:\Users\drt\SkyDrive\Programmierung\nodejs\silkveil\node_modules\connect\lib\patch.js:63:22)
    at next (C:\Users\drt\SkyDrive\Programmierung\nodejs\silkveil\node_modules\connect\lib\proto.js:156:13)
    at Object.handle (C:\Users\drt\SkyDrive\Programmierung\nodejs\silkveil\connectSampleApp.js:13:3)
    at next (C:\Users\drt\SkyDrive\Programmierung\nodejs\silkveil\node_modules\connect\lib\proto.js:193:15)
    at Function.app.handle (C:\Users\drt\SkyDrive\Programmierung\nodejs\silkveil\node_modules\connect\lib\proto.js:201:3)
    at Server.app (C:\Users\drt\SkyDrive\Programmierung\nodejs\silkveil\node_modules\connect\lib\connect.js:65:37)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)

当我将“writeHead”代码移动到最后一个回调时,一切都很好:

.use(function(req, res){
        console.log('reached the tail of the "chain of responsibility!!!');
        res.writeHead(200, { 'content-type': 'text/html' });
        res.write('response powered by SIMPLE connect-object as middelware');
        res.end();
    });

所以我的问题是:使用 Connect 时是否只允许在最后一个回调中使用 writeHead/write?

最佳答案

检查另一个问题。 res.writeHead 基本上在调用 res.writeHead 后, header 就无法再修改了,而 next 方法会尝试修改 header ,从而导致异常。

因此您可以在第一个连接回调中修改 header ,但不允许写入正文(res.write)。以下代码应该可以正常工作。简而言之,您可以修改 header ,但不能刷新它们。

var http = require('http');
var connect = require('connect');

var simpleApp = connect();

simpleApp
    .use(function(req, res, next){
        res.statusCode = 200;
        res.setHeader( 'content-type', 'text/html' );

        console.log('Pre');
        next();
        console.log('Post');
    })
    .use(function(req, res, next){
        console.log('I am the header guy!');
        next();
    })
    .use('/admin', function(req, res, next){
        console.log('someone entered the admin area....');
        next();
    })
    .use(function(req, res){
        res.write('response powered by SIMPLE connect-object as middelware');
        console.log('reached the tail of the "chain of responsibility!!!');
        res.end();
    });

http.createServer(simpleApp).listen(process.env.PORT || 3000);

console.log('Running on port "' + (process.env.PORT || 3000) + '"');

// just a save-guard to stop the process after some time...
setTimeout(function(){
    process.exit(0);
}, 20000);

关于node.js - Nodejs : Connect and "writeHead" in first connect-callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19980426/

相关文章:

node.js - Express 3、Apache 代理和基本路径

javascript - 这是什么JS设计模式?

node.js - Meteor:无法部署,需要 Node v0.10.40 或更高版本

javascript - 如何在 lodash 的 map 函数中获取 Mongoose findById 响应?

node.js - 我们如何使用 Node.js Connect 映射路径?

node.js - 快速/连接 : is it possible to use the static middleware several times?

node.js - res.render(template)时使用node和express服务get(/foo/:bar) requests,,template中所有相对链接都是相对于/foo而不是/

node.js - haxe/flash : Capture webcam to disc with node. js - 可能吗?

node.js - 如何在 Windows 和 Unix 上运行 npm sh 脚本

node.js - sequelize-auto TypeError : connection. query(...).on 不是函数