Logging raw request body in express app not working
jdboris
HOBBYOP

a year ago

In my express app deployed to railway, in the following middleware, the first console.log outputs the request body correctly but the second one outputs an empty string. What gives? It works fine locally.

Project ID:d414d846-9e9b-45fe-b52f-1fa28e8426ae
Node image:node:22.12.0-alpine
Express version:5.0.1

export default function requestLogger(
  req,
  res,
  next
) {
  express.raw({ type: "*/*" })(req, res, () => {
    const body = req.body.toString();
    console.log("body: ", body);
    console.log(body);
  });

  next();
}
Solved

18 Replies

brody
EMPLOYEE

a year ago

I'm sorry but we wouldn't be able to provide coding support here


jdboris
HOBBYOP

a year ago

I'm not asking for coding support. This is a bug in railway's logging


dev
MODERATOR

a year ago

Perhaps it's accidentally turning into structured logs

click on the + button on the side, your info may be appearing there

1321321973292204000


brody
EMPLOYEE

a year ago

it is not a bug on Railway's side, it is also not productive to place the blame on the platform


jdboris
HOBBYOP

a year ago

this is all I see

1321322456404983800


dev
MODERATOR

a year ago

then it is turning into structured logs


jdboris
HOBBYOP

a year ago

how do you know it's not a bug on railway's side. Do you know the solution?


dev
MODERATOR

a year ago

you can see the {"x": 5} is attached as metadata into the empty log string


jdboris
HOBBYOP

a year ago

oh I see


jdboris
HOBBYOP

a year ago

thanks


dev
MODERATOR

a year ago

no problem


jdboris
HOBBYOP

a year ago

still not coding support FYI @Brody


brody
EMPLOYEE

a year ago

maybe not, but explaining JSON is definitely not platform support


jdboris
HOBBYOP

a year ago

by the way, can I disable this structured logging thing? I'll check the docs


jdboris
HOBBYOP

a year ago

explaining why I need to click an extra button to see the result of console.log in the logs is platform support


dev
MODERATOR

a year ago

Not directly, just make sure you're not outputting valid JSON directly, or use a logging library like winston


brody
EMPLOYEE

a year ago

haha no not really


brody
EMPLOYEE

a year ago

!s


Loading...