Skip to content

Commit

Permalink
Include req.params and req.query when available (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansamines authored Apr 5, 2022
1 parent 50907a5 commit 75bff4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ The default `request` serializer. Returns and object:
// the value filled.
method: 'string',
url: 'string', // the request pathname (as per req.url in core HTTP)
query: 'object', // the request query (as per req.query in express or hapi)
params: 'object', // the request params (as per req.params in express or hapi)
headers: Object, // a reference to the `headers` object from the request
// (as per req.headers in core HTTP)
remoteAddress: 'string',
Expand Down
11 changes: 9 additions & 2 deletions lib/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ function reqSerializer (req) {
// req.originalUrl is for expressjs compat.
if (req.originalUrl) {
_req.url = req.originalUrl
_req.query = req.query
_req.params = req.params
} else {
const path = req.path
// path for safe hapi compat.
_req.url = typeof path === 'string' ? path : (req.url ? req.url.path || req.url : undefined)
}

if (req.query) {
_req.query = req.query
}

if (req.params) {
_req.params = req.params
}

_req.headers = req.headers
_req.remoteAddress = connection && connection.remoteAddress
_req.remotePort = connection && connection.remotePort
Expand Down
2 changes: 0 additions & 2 deletions test/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ test('req.query is available', function (t) {
t.teardown(() => server.close())

function handler (req, res) {
req.originalUrl = '/test'
req.query = '/foo?bar=foobar&bar=foo'
const serialized = serializers.reqSerializer(req)
t.equal(serialized.query, '/foo?bar=foobar&bar=foo')
Expand All @@ -426,7 +425,6 @@ test('req.params is available', function (t) {
t.teardown(() => server.close())

function handler (req, res) {
req.originalUrl = '/test'
req.params = '/foo/bar'
const serialized = serializers.reqSerializer(req)
t.equal(serialized.params, '/foo/bar')
Expand Down

0 comments on commit 75bff4e

Please sign in to comment.