21 lines
536 B
JavaScript
21 lines
536 B
JavaScript
// middleware/logApi.js
|
|
const chalk = require('chalk').default;
|
|
|
|
module.exports = function (req, res, next) {
|
|
if (global.DEBUG_MODE) {
|
|
const start = Date.now();
|
|
res.on('finish', () => {
|
|
const duration = Date.now() - start;
|
|
console.log(
|
|
chalk.magenta('[API]'),
|
|
chalk.cyan(req.method),
|
|
chalk.white(req.originalUrl),
|
|
chalk.yellow(res.statusCode),
|
|
chalk.gray(`${duration}ms`),
|
|
req.headers['x-api-key'] ? chalk.green('[x-api-key]') : ''
|
|
);
|
|
});
|
|
}
|
|
next();
|
|
};
|