Good morning,
I am trying to log user input from user when using morgan and express. What I am specifically trying to log is noted in this image: (The user posted a request with an object that includes two key/value pairs “name” and “number”)
How would I go about logging this? Custom tokens or is there a predefined method I could use?
Advertisement
Answer
I found the solution, very simple:
JavaScript
x
8
1
app.use(express.json())
2
3
morgan.token("code", function getCode(req) {
4
return JSON.stringify(req.body);
5
});
6
7
app.use(morgan(':method :url :response-time :code'))
8
JSON.stringify(req.body)
was what I was looking for!