Node.js:连接并“ writeHead”在第一次连接回调中 [英] Nodejs: Connect and "writeHead" in first connect-callback

查看:122
本文介绍了Node.js:连接并“ writeHead”在第一次连接回调中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在连接的第一个回调中调用 res.writeHead(...)时,我总是收到错误 错误:发送标头后无法设置标头。。 / p>

I receive always the error "Error: Can't set headers after they are sent." when calling "res.writeHead(...)" in the very first callback of connect:

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代码,一切都很好:

When I move the "writeHead"-code into the very last callback everything is fine:

.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();
    });

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

So my question is: Is it only permitted to use writeHead /write in the very last callback when using connect?

推荐答案

检查其他问题。 res.writeHead
基本上调用 res.writeHead 之后,无法再修改标头,而 next 方法将尝试修改

check the other question. res.writeHead Basically after res.writeHead is called, the header could not be modified any more, while next method would try to modify the header which will cause the exception.

因此您可以在第一个connect回调中修改标题,但不允许编写正文( res.write )。以下代码应正常工作。简而言之,您可以修改标头,但不能清除标头。

So you can modify the header in the first connect callback, but you are not allowed to write the body(res.write). Following code should work correctly. In short, you can modify the header but not to flush them.

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:连接并“ writeHead”在第一次连接回调中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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