From 5b20a47a9c0c9396212d98f80c204f3808710867 Mon Sep 17 00:00:00 2001 From: Rocky Murdoch Date: Tue, 7 Jan 2025 18:30:13 +0200 Subject: [PATCH] refactor: move request logger middleware to express level --- src/index.js | 6 ++++++ src/routes/capacity.js | 5 +---- src/routes/middleware.js | 22 ---------------------- src/routes/pricing.js | 2 -- src/routes/quote.js | 5 +---- 5 files changed, 8 insertions(+), 32 deletions(-) delete mode 100644 src/routes/middleware.js diff --git a/src/index.js b/src/index.js index defa44f..c64d4af 100644 --- a/src/index.js +++ b/src/index.js @@ -43,6 +43,12 @@ const main = async () => { next(); }); + // log incoming requests + app.use((req, res, next) => { + console.info(`${req.method} ${req.originalUrl}`); + next(); + }); + // initiate routes app.use('/v2', capacityRouter); app.use('/v2', quoteRouter); diff --git a/src/routes/capacity.js b/src/routes/capacity.js index 3034652..7a58067 100644 --- a/src/routes/capacity.js +++ b/src/routes/capacity.js @@ -3,7 +3,6 @@ const { inspect } = require('node:util'); const { ethers, BigNumber } = require('ethers'); const express = require('express'); -const { requestLogger } = require('./middleware'); const { getAllProductCapacities, getProductCapacity, @@ -13,10 +12,8 @@ const { const { SECONDS_PER_DAY } = require('../lib/constants'); const { asyncRoute } = require('../lib/helpers'); -const { formatUnits } = ethers.utils; - const router = express.Router(); -router.use(requestLogger); +const { formatUnits } = ethers.utils; const formatCapacityResult = capacity => ({ productId: capacity.productId, diff --git a/src/routes/middleware.js b/src/routes/middleware.js deleted file mode 100644 index 731cd45..0000000 --- a/src/routes/middleware.js +++ /dev/null @@ -1,22 +0,0 @@ -const { inspect } = require('node:util'); - -/** - * Request Logger Middleware - * - * Logs incoming requests with method, URL, params, and query. - * - * @param {import('express').Request} req - The Express request object - * @param {import('express').Response} res - The Express response object - * @param {Function} next - The next middleware function - */ -const requestLogger = (req, res, next) => { - const params = Object.keys(req.params).length ? `\nparams: ${inspect(req.params, { depth: null })}` : ''; - const query = Object.keys(req.query).length ? `\nquery: ${inspect(req.query, { depth: null })}` : ''; - - console.info(`${req.method} ${req.baseUrl}${req.route?.path || req.path}${params}${query}`); - next(); -}; - -module.exports = { - requestLogger, -}; diff --git a/src/routes/pricing.js b/src/routes/pricing.js index 0bbb73d..0e69be0 100644 --- a/src/routes/pricing.js +++ b/src/routes/pricing.js @@ -2,12 +2,10 @@ const { inspect } = require('node:util'); const express = require('express'); -const { requestLogger } = require('./middleware'); const { asyncRoute } = require('../lib/helpers'); const { pricingEngine } = require('../lib/pricingEngine'); const router = express.Router(); -router.use(requestLogger); const formatPricingResult = pricing => ({ productId: pricing.productId, diff --git a/src/routes/quote.js b/src/routes/quote.js index 483bed8..204c591 100644 --- a/src/routes/quote.js +++ b/src/routes/quote.js @@ -3,16 +3,13 @@ const { inspect } = require('node:util'); const { BigNumber, ethers } = require('ethers'); const express = require('express'); -const { requestLogger } = require('./middleware'); const { TARGET_PRICE_DENOMINATOR } = require('../lib/constants'); const { asyncRoute } = require('../lib/helpers'); const { quoteEngine } = require('../lib/quoteEngine'); const { selectAsset } = require('../store/selectors'); -const { Zero } = ethers.constants; - const router = express.Router(); -router.use(requestLogger); +const { Zero } = ethers.constants; /** * @openapi