From 37d2647f82b0996ce20e58f44cea820d01762513 Mon Sep 17 00:00:00 2001 From: Dmitry Kondar Date: Sat, 27 Jan 2024 20:29:09 +0200 Subject: [PATCH] fix favicon-related bug --- examples/cookie-sessions/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/cookie-sessions/index.js b/examples/cookie-sessions/index.js index 01c731c1c8..b6730f0879 100644 --- a/examples/cookie-sessions/index.js +++ b/examples/cookie-sessions/index.js @@ -16,9 +16,15 @@ app.use(cookieSession({ secret: 'manny is cool' })); app.use(count); // custom middleware -function count(req, res) { - req.session.count = (req.session.count || 0) + 1 - res.send('viewed ' + req.session.count + ' times\n') +function count(req, res, next) { + // without this check, a request to /favicon that + // is made by most browsers will also increment the counter + if (req.path.startsWith('/favicon.ico')) { + return next(); + } else { + req.session.count = (req.session.count || 0) + 1; + res.send('viewed ' + req.session.count + ' times\n'); + } } /* istanbul ignore next */