Files
Parchment/flame/util.js

40 lines
1.4 KiB
JavaScript

import moment from 'moment'
import chalk from 'chalk'
import paths from 'path'
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = paths.dirname(__filename);
export default class util {
static APP_PATH = paths.join(__dirname, "..")
static CAVE_PATH = paths.join(__dirname, "../cave")
static FLAME_PATH = __dirname
static FORMS_PATH = paths.join(__dirname, "../forms")
static logRequest(req, res, next) {
const formattedDate = moment().format('M.D');
const formattedTime = moment().format('h:mma');
if(req.url.includes("/api/")) {
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
} else {
if(req.url === "/")
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
}
next();
}
static logResponse(req, res, next) {
const originalSend = res.send;
res.send = function (body) {
if(res.statusCode >= 400) {
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
} else {
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
}
originalSend.call(this, body);
};
next();
}
}