mirror of
https://github.com/wassname/talk.git
synced 2026-07-05 18:49:18 +08:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
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);
|
|
};
|