gpt4 book ai didi

node.js - 在发送给客户端之前劫持响应

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:48 24 4
gpt4 key购买 nike

我有以下代码用作中间件

module.exports=function(callback) {
callbacks.push(callback);
return function(req,res,next) {
if (!res.hijacked) {
res.hijacked=true;
} else {
return next();
}
var send=res.send;
res.send=function(data) {
var body=data instanceof Buffer ? data.toString() : data;
var requests=[];
requests.push(function(next) {
callbacks[0](req,res)(body,doneWrapper(body,next));
});
for (var i=1;i<callbacks.length;i++) {
var hijackCallback=callbacks[i];
requests.push(function(result,next) {
hijackCallback(req,res)(result,doneWrapper(result,next));
});
}
var that=this;
async.waterfall(requests,function(err,result) {
send.call(that,result);
requests=null;
body=null;
that=null;
});
};
next();
};
};

使用示例如下:

module.exports=function() {
return hijack(function() {
return function(result,done) {
var json={};
try {
json=JSON.parse(result);
} catch(e) {
return done();
}
if (!_.isArray(json)) {
return done();
}
var sorted=_(json).sortBy(function(item) {
if (_.isObject(item.information)) {
return item.information.rangeIndex1 || 999;
} else {
return 1001;
}
});
done(sorted);
}
});
};

它最初作为路由中的中间件工作得很好。

但是,当我尝试将其设置为 app.use(hijackMiddleware()) 时。出了点问题,我收到了发送后无法设置 header 错误。

不过,在路由中用作中间件时没有问题。

最佳答案

您是否考虑过使用 express-interceptor ?真的很容易use :

var express     = require('express');
var cheerio = require('cheerio');
var interceptor = require('express-interceptor');

var app = express();

var finalParagraphInterceptor = interceptor(function(req, res){
return {
// Only HTML responses will be intercepted
isInterceptable: function(){
return /text\/html/.test(res.get('Content-Type'));
},
// Appends a paragraph at the end of the response body
intercept: function(body, send) {
var $document = cheerio.load(body);
$document('body').append('<p>From interceptor!</p>');

send($document.html());
}
};
})

// Add the interceptor middleware
app.use(finalParagraphInterceptor);

app.use(express.static(__dirname + '/public/'));

app.listen(3000);

关于node.js - 在发送给客户端之前劫持响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com