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
44 lines
987 B
JavaScript
import { Router } from 'express'
|
|
|
|
import chalk from 'chalk'
|
|
import { toLocalISO } from '../utils/util.js'
|
|
|
|
export class StatusRouter extends Router {
|
|
constructor() {
|
|
super()
|
|
|
|
this.get('/', (req, res) => {
|
|
res.json({ status: 'ok' })
|
|
})
|
|
}
|
|
}
|
|
|
|
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')
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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}`)
|
|
}
|