Nodejs:第一个connect-callback中的Connect和“writeHead”

t3psigkw  于 2023-06-05  发布在  Node.js
关注(0)|答案(2)|浏览(155)

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

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?

ifsvaxew

ifsvaxew1#

检查其他问题。res.writeHead基本上在调用res.writeHead之后,头文件就不能再修改了,而next方法会尝试修改头文件,这会导致异常。
因此,您可以在第一个连接回调中修改头部,但不允许写入主体(res.write)。下面的代码应该可以正常工作。简而言之,您可以修改标题,但不能刷新它们。

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);
lvmkulzt

lvmkulzt2#

您在代码的第一部分中忘记了这一行:

res.end();

如果这不起作用,TJ Holowaychuk在这里发表评论:https://github.com/senchalabs/connect/issues/683#issuecomment-10018892

相关问题