forked from buerokratt/DataMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f726bca
commit d4501dc
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @param res Original Response Object | ||
* @param send Original UNMODIFIED res.send function | ||
* @return A patched res.send which takes the send content, binds it to contentBody on | ||
* the res and then calls the original res.send after restoring it | ||
*/ | ||
const resDotSendInterceptor = (res, send) => (content) => { | ||
res.contentBody = content; | ||
res.send = send; | ||
res.send(content); | ||
}; | ||
|
||
export const requestLoggerMiddleware = | ||
({ logger }) => | ||
(req, res, next) => { | ||
logger( | ||
`Request: {method: ${req.method}, url: ${ | ||
req.url | ||
}, params: ${JSON.stringify(req.params)}, query: ${JSON.stringify( | ||
req.query | ||
)}, body: ${JSON.stringify(req.body)}` | ||
); | ||
res.send = resDotSendInterceptor(res, res.send); | ||
res.on("finish", () => { | ||
logger( | ||
`Response: {statusCode: ${res.statusCode}, responseData: ${res.contentBody}}` | ||
); | ||
}); | ||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters