kodex-music-catalog/logger.js
2023-09-30 04:14:59 +03:00

31 lines
879 B
JavaScript

const config = require('./config-handler');
function log (date, req, ip, res) {
// console.log(req);
const requestBody = !req.byteBody.toString('utf-8') ? '' : `
~~~~~~~~~~~~~~
[REQUEST BODY]
~~~~~~~~~~~~~~
${req.byteBody.toString('utf-8')}`;
let action = `HTTP ${req.httpVersion} ${req.method} ${req.url}
~~~~~~~~~
[HEADERS]
~~~~~~~~~
${Object.entries(req.headers).map(([header, value]) => header + ": " + value).join('\n')}${requestBody}`;
console.log(res);
// console.log(res.headers);
let response = 'NULL';
console.log(`================================\nREPORT\n================================\n\nIP: ${ip}\n----------------\nACTION:\n----------------\n${action}\n----------------\nRESPONSE:\n----------------\n${response}`);
}
module.exports = async (req, res, next) => {
// console.log('ip', req.ip);
log(
new Date(),
req,
req.ip,
res
);
next();
};