Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't log in to response to the body #340

Closed
2 tasks done
1340145680 opened this issue Jul 1, 2024 · 3 comments
Closed
2 tasks done

I can't log in to response to the body #340

1340145680 opened this issue Jul 1, 2024 · 3 comments

Comments

@1340145680
Copy link

1340145680 commented Jul 1, 2024

The base framework I use is express

I modified my code according to the Add log request body example

const httpLogger = pinoHttp({
  level: "trace",
  quietReqLogger: true,
  serializers: {
    req(req) {
      console.log(req);
      req.body = req.raw.body;
      return req;
    },
  },
  })

I get the following data on my console, where the raw data type is IncomingMessage

console image

There is indeed body in raw

raw data view

But I can't get it according to the docs

req(req) {
    console.log("req.raw.body:", req.raw.body);
    req.body = req.raw.body;
    return req;
}

console image

@1340145680
Copy link
Author

@mcollina

@jsumners
Copy link
Member

jsumners commented Jul 1, 2024

Please provide a minimal reproducible example. Doing so will help us diagnose your issue. It should be the bare minimum code needed to trigger the issue, and easily runnable without any changes or extra code.

You may use a GitHub repository to host the code if it is too much to fit in a code block (or two).

@1340145680
Copy link
Author

1340145680 commented Jul 2, 2024

Sorry, I got the middleware mixed up.

error code

app.use(logger); //pino logger
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

correct code

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(logger); //pino logger

The logger must be parsed after the body middleware of the incoming request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants