You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
987 B
JavaScript

2 years ago
import { Router } from 'express'
import chalk from 'chalk'
import { toLocalISO } from '../utils/util.js'
2 years ago
export class StatusRouter extends Router {
constructor() {
super()
2 years ago
this.get('/', (req, res) => {
res.json({ status: 'ok' })
})
}
}
2 years ago
export class PingRouter extends Router {
constructor() {
super()
this.post('/', (req, res) => {
if (req.body?.message === 'ping') {
res.json('pong')
} else {
res.status(400)
res.json('Invalid request')
}
})
}
2 years ago
}
2 years ago
export const loggingMiddleware = (req, res, next) => {
next()
const coloredStatusCode = [
chalk.gray,
chalk.green,
chalk.blueBright,
chalk.red,
chalk.redBright,
][Math.floor(res.statusCode / 100) - 1](res.statusCode)
console.log(`${toLocalISO(new Date())} | ${req.method} ${req.originalUrl} ${coloredStatusCode}`)
}