Logging raw request body in express app not working
jdboris
HOBBYOP

2 years 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

2 years ago

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


jdboris
HOBBYOP

2 years ago

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


2 years ago

Perhaps it's accidentally turning into structured logs

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

1321321973292204052


2 years ago

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


jdboris
HOBBYOP

2 years ago

this is all I see

1321322456404983819


2 years ago

then it is turning into structured logs


jdboris
HOBBYOP

2 years ago

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


2 years ago

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


jdboris
HOBBYOP

2 years ago

oh I see


jdboris
HOBBYOP

2 years ago

thanks


2 years ago

no problem


jdboris
HOBBYOP

2 years ago

still not coding support FYI @Brody


2 years ago

maybe not, but explaining JSON is definitely not platform support


jdboris
HOBBYOP

2 years ago

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


jdboris
HOBBYOP

2 years ago

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


2 years ago

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


2 years ago

haha no not really


2 years ago

!s


Welcome!

Sign in to your Railway account to join the conversation.

Loading...