mirror of
https://github.com/wassname/talk.git
synced 2026-08-18 12:30:39 +08:00
initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { RequestHandler, ErrorRequestHandler } from 'express';
|
||||
import logger from '../../logger';
|
||||
import now from 'performance-now';
|
||||
|
||||
export const access: RequestHandler = (req, res, next) => {
|
||||
const startTime = now();
|
||||
const end = res.end;
|
||||
res.end = (chunk: any, encodingOrCb?: string | Function, cb?: Function) => {
|
||||
// Compute the end time.
|
||||
const responseTime = Math.round(now() - startTime);
|
||||
|
||||
// Get some extra goodies from the request.
|
||||
const userAgent = req.get('User-Agent');
|
||||
|
||||
// Reattach the old end, and finish.
|
||||
res.end = end;
|
||||
if (typeof encodingOrCb === 'function') {
|
||||
res.end(chunk, encodingOrCb);
|
||||
} else {
|
||||
res.end(chunk, encodingOrCb, cb);
|
||||
}
|
||||
|
||||
// Log this out.
|
||||
logger.info(
|
||||
{
|
||||
// traceID: req.id,
|
||||
url: req.originalUrl || req.url,
|
||||
method: req.method,
|
||||
statusCode: res.statusCode,
|
||||
userAgent,
|
||||
responseTime,
|
||||
},
|
||||
'http request'
|
||||
);
|
||||
};
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
export const error: ErrorRequestHandler = (err, req, res, next) => {
|
||||
logger.error({ err }, 'http error');
|
||||
next(err);
|
||||
};
|
||||
Reference in New Issue
Block a user