0

I am trying to add logging with pino to an existing node.js web application, that uses express.js and Postgresql. However, some HTTP post request are not being logged. It seems to only happen with HTTP post request responding with a redirect. It also only happens then requesting by Safari. The problem does not occur then using Chrome or curl.

I am produced a minimal example here under, that reproduces the problem. As in my main project it does reproduce the problem when using Safari on my laptop, but not when using Chrome or curl. If I remove the usage of connect-pg-simple the problem disappears.

No error message what so ever is given.

import { default as express } from 'express'
import { default as pinoHttp } from 'pino-http'
import { default as session } from 'express-session';
import { default as sessionStore } from 'connect-pg-simple';

import { default as pg } from 'pg';
const Pool = pg.Pool
const pool = new Pool()

const reqLogger = pinoHttp({
    level: 'info',
    enabled: true
})

const app = express()

app.use(session({
    secret: 'secret',
    name: 'sessionId',
    resave: false,
    rolling: true,
    saveUninitialized: true,
    store: new (sessionStore(session))({
        pool: pool
    }),
    cookie: {
        secure: 'auto',
        maxAge: 30 * 24 * 60 * 60 * 1000
    }
}))

app.use(reqLogger)

app.get('/', (req, res, next) => {
    const html = `
        <form method="POST" action="/">
            <div>
                <label for="floatingInput">Username</label>
                <input name="username" type="text" id="floatingInput">
            </div>
            <div>
                <label for="floatingPassword">Password</label>
                <input name="password" type="password" id="floatingPassword">
            </div>

            <button type="submit">Log in</button>
        </form>`
    res.send(html)
})

app.post('/', (req, res, next) => {
    res.redirect('/')
})

app.listen(8081, () => {
    console.log('Server running')
})
rlp
  • 513
  • 2
  • 7
  • 16
  • Unclear if that helps, but sessions are not automatically saved upon a redirect, see [here](https://stackoverflow.com/a/72564300/16462950). – Heiko Theißen Dec 10 '22 at 11:03
  • @HeikoTheißen it makes no different calling the function res.session.save as described in your link. It also would not explain the browser/client dependency. – rlp Dec 10 '22 at 11:12

0 Answers0