2

I have written a web server in nodejs. Most of the time I am receiving a message from one service, doing something, and sending a message to another service. I am in the middle of all the communication.

Sometimes, the communication fails. I am trying to debug what's going on. I would like to examine the request that comes in.

I have a node service, written in express. I have routes, and the routes are passed a req object and a resp object. I should be able to just print out the req object. Problem solved!

But JSON.stringify throws an error. util.inspect doesn't throw an error, but many property values are marked [circular]. The actual property value isn't shown.

When I console.log(req.body) it prints undefined. When I look at req.body using util.inspect, it prints body: {}

I have the feeling the framework is hiding things from me. I don't know how to get the information without it being prettified.

At the tcp/ip level, it's too detailed. At the application level, it's not detailed enough. But at the http level, it should be just right. The request that is received is just text. I should be able to print it out.

I tried using Charles, but I'm having trouble configuring it.

Surely, other people have wanted to see the request as it comes in, before the framework massaged it. How did they do it?

user2171796
  • 377
  • 1
  • 13

2 Answers2

1

you can use morgan module, it's a HTTP request logger middleware for node.js

Zoe Bi
  • 329
  • 2
  • 7
  • Hi, Nokey. I tried Morgan. Seems to want to give me "boiled down" info. I used "Apache composite" among others, and couldn't get what I wanted. In particular, the data I sent (which I believe was in the request body) didn't show up. I sent "city=chicago" and didn't see it in the log. I know it got through, because when I send "city=atlanta" I get a different result on the other side. – user2171796 Sep 29 '16 at 11:04
0

I made a more specific question, using a lower level of the node stack of middleware. I got an answer there:
Where did the information I passed in go?
Here is the discussion of how node came to be designed this way:
Node.js - get raw request body using Express
Basically, there used to be a rawBody attribute of the request object in node. People took it out. To accomplish the same thing requires a little bit of code.

Community
  • 1
  • 1
user2171796
  • 377
  • 1
  • 13