我想創建express.use一個中間件來獲取從所有端點發送的資料并將其用于快取。但是我無法使用res.on('finish', cb). 甚至有這樣的事情嗎?
謝謝
uj5u.com熱心網友回復:
我將結束我的問題,因為我自己找到了方法:
app.use((req, res, next) => {
const send = res.send;
res.send = (data) => {
res.send = send; // this line is important not to have an infinite loop
// do something with `data`
return res.send(data);
};
next();
});
uj5u.com熱心網友回復:
添加中間件并使用您的自定義函式覆寫現有的res.send函式,如下所示
app.use((req, res, next) => {
const { send } = res;
res.send = (data) => {
// Store in cache
return send(data);
};
next();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/403694.html
標籤:
