Node.js表达:在发送之前获取HTTP标头

时间:2022-08-26 19:01:11

I am currently making an application in Node.js, with the Express web server plug-in. I want to count the total data sent by the web server. To do this, I need to get the 'Content-Length' field of an outgoing HTTP header. However, I need to do this right after the data is added.

我目前正在使用Express Web服务器插件在Node.js中创建一个应用程序。我想计算Web服务器发送的总数据。为此,我需要获取传出HTTP标头的“Content-Length”字段。但是,我需要在添加数据后立即执行此操作。

If I need to alter core Express scripts, can somebody tell me which file this is contained in?

如果我需要更改核心Express脚本,有人可以告诉我包含哪个文件?

3 个解决方案

#1


2  

If you just want to count it, you can use a middleware for that:

如果您只想计算它,可以使用中间件:

var totalBytes = 0;
app.use(function(req, res, next) {
  res.on('finish', function() {
    totalBytes += Number(res.get('content-length') || 0);
  });
  next();
});

You have to include very early in the middleware stack, before any other middleware whose contents you want to count.

您必须在中间件堆栈中尽早包含在您要计算其内容的任何其他中间件之前。

Also, this doesn't count any streamed data, for which no Content-Length header is set.

此外,这不计算任何流数据,因为没有设置Content-Length标头。

#2


1  

You could add middleware to monkey-patch the response methods. It’s ugly but better than altering core Express files.

您可以添加中间件来修补响应方法。它很丑,但比改变核心Express文件更好。

This calculates total body bytes for standard and streamed responses. Place this before any other app.use() directives.

这计算标准和流式响应的总体字节数。将其放在任何其他app.use()指令之前。

app.use(function(req, res, next) {
  res.totalLength = 0;

  var realResWrite = res.write;
  res.write = function(chunk, encoding) {
    res.totalLength += chunk.length;
    realResWrite.call(res, chunk, encoding);
  };

  var realResEnd = res.end;
  res.end = function(data, encoding) {
    if (data) { res.totalLength += data.length; }
    console.log('*** body bytes sent:', res.totalLength);
    realResEnd.call(res, data, encoding);
  };

  next();
});

#3


0  

If you want to count it on each request then you can try below code!!!

如果你想在每个请求上计算它,那么你可以尝试下面的代码!

var totalByte=0;
app.all('*', function(req,res, next){
    res.on('finish', function() {
        totalByte = parseInt(res.getHeader('Content-Length'), 10);
        if(isNaN(totalByte))
            totalByte = 0;
   });
    next(); 
});

Please remember totalByte is a global variable here and it added value on each request

请记住totalByte在这里是一个全局变量,它为每个请求增加了价值

#1


2  

If you just want to count it, you can use a middleware for that:

如果您只想计算它,可以使用中间件:

var totalBytes = 0;
app.use(function(req, res, next) {
  res.on('finish', function() {
    totalBytes += Number(res.get('content-length') || 0);
  });
  next();
});

You have to include very early in the middleware stack, before any other middleware whose contents you want to count.

您必须在中间件堆栈中尽早包含在您要计算其内容的任何其他中间件之前。

Also, this doesn't count any streamed data, for which no Content-Length header is set.

此外,这不计算任何流数据,因为没有设置Content-Length标头。

#2


1  

You could add middleware to monkey-patch the response methods. It’s ugly but better than altering core Express files.

您可以添加中间件来修补响应方法。它很丑,但比改变核心Express文件更好。

This calculates total body bytes for standard and streamed responses. Place this before any other app.use() directives.

这计算标准和流式响应的总体字节数。将其放在任何其他app.use()指令之前。

app.use(function(req, res, next) {
  res.totalLength = 0;

  var realResWrite = res.write;
  res.write = function(chunk, encoding) {
    res.totalLength += chunk.length;
    realResWrite.call(res, chunk, encoding);
  };

  var realResEnd = res.end;
  res.end = function(data, encoding) {
    if (data) { res.totalLength += data.length; }
    console.log('*** body bytes sent:', res.totalLength);
    realResEnd.call(res, data, encoding);
  };

  next();
});

#3


0  

If you want to count it on each request then you can try below code!!!

如果你想在每个请求上计算它,那么你可以尝试下面的代码!

var totalByte=0;
app.all('*', function(req,res, next){
    res.on('finish', function() {
        totalByte = parseInt(res.getHeader('Content-Length'), 10);
        if(isNaN(totalByte))
            totalByte = 0;
   });
    next(); 
});

Please remember totalByte is a global variable here and it added value on each request

请记住totalByte在这里是一个全局变量,它为每个请求增加了价值