Home > Enterprise >  I'm unable to access content-length of response headers in app.use() middleware
I'm unable to access content-length of response headers in app.use() middleware

Time:06-06

I am trying to access content-length of response headers in app.use() middleware:

const app = express();
app.use((req, res, next) => {

    //how to get content-length of res
    console.log("content-length", res);  //not able to access content-length
    next(); 

})

CodePudding user response:

response.headers.get('content-length')

CodePudding user response:

The Content-Length header is of course unknown until the response has been finished. You must therefore execute the code that accesses it in the finish event of the response object:

app.use(function(req, res, next) {
  res.on("finish", function() {
    console.log(res.get("content-length"));
  });
  next();
});
  • Related