Mir*_*toš 5 javascript logging http node.js
我想写一个通用(访问)日志格式的快速中间件打印HTTP访问日志。此格式的最后一列是响应正文的大小(以字节为单位)。
如何确定 Node.js 服务器产生的 HTTP 响应的大小?
不起作用的示例:
app.use(function(req, res, next) {
var bytes = 0;
res.on('data', function(chunk) {
bytes += chunk.length;
});
res.on('finish', function() {
// `bytes` is the response size
console.log(/* common log entry */);
});
});
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,例如 Hapi 的common-log总是-作为大小发出。这是否意味着无法获得尺寸?
小智 -4
试试这个代码:
app.use(function(req, res, next) {
var data = [];
res.on('data', function(chunk) {
data.push(chunk);
});
res.on('finish', function() {
console.log( Buffer.concat(data).toString().length() );
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3971 次 |
| 最近记录: |