hapijs / hapijs/hapi-pino

Question:How to get the response body in hapi-pino logs?

Open
#121 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
147
Forks
56
PR merge metrics
No merged PRs in 30d

Description

My code is a follows:
```
'use strict';

const hapi = require('@hapi/hapi')
const HapiPino = require('hapi-pino')

const server = hapi.server({ port: 3000 })

server.route([
{
method: 'POST',
path: '/response',
handler:(request, h) => {
return {'test':'Resp Msg'}
}
},
]);

function resSerializer(res) {
return {
body:res.raw.body
};
}

(async () => {
try {
await server.register({
plugin: HapiPino,
options: {
logPayload: true,
serializers:{
res: resSerializer
},
}
})
await server.start()
console.log('Server started successfully')
} catch (err) {
console.error(err)
}
})()
```
Logs are as shown as below:
```
{"level":30,"time":1598811397374,"pid":15504,"hostname":"IN5CG7192BNG","req":{"id":"1598811397349:IN5CG7192BNG:15504:kehf0ria:10000","method":"post","url":"/response","headers":{"content-type":"application/json","user-agent":"PostmanRuntime/7.6.0","accept":"*/*","host":"localhost:3000","accept-encoding":"gzip, deflate","content-length":"50","connection":"keep-alive"},"remoteAddress":"127.0.0.1","remotePort":61801},"payload":{"test1":"JVA","test2":"JVA","test3":"JVA"},"req":{"id":"1598811397349:IN5CG7192BNG:15504:kehf0ria:10000","method":"post","url":"/response","headers":{"content-type":"application/json","user-agent":"PostmanRuntime/7.6.0","accept":"*/*","host":"localhost:3000","accept-encoding":"gzip, deflate","content-length":"50","connection":"keep-alive"},"remoteAddress":"127.0.0.1","remotePort":61801},"res":{},"responseTime":24,"msg":"request completed"}
```

Response body is showing as ```"res":{}```

How to get the response body value, which is ```{'test':'Resp Msg'}```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.