Skip to content

Commit c8cf0b9

Browse files
committed
fix: capture route from base on express app.use
1 parent 3adbadf commit c8cf0b9

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ See the <<upgrade-to-v4>> guide.
5050
5151
[float]
5252
===== Bug fixes
53+
* The problem of route name not being captured in Express.Use direct use in middleware has been resolved.
5354
5455
[float]
5556
===== Chores

lib/instrumentation/express-utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ function getPathFromRequest(req, useBase, usePathAsTransactionName) {
4848
return path ? join([path, route]) : route;
4949
} else if (path && (path !== '/' || useBase)) {
5050
return path;
51+
} else if (req.baseUrl && req.baseUrl !== '/') {
52+
return req.baseUrl;
5153
}
5254

5355
if (usePathAsTransactionName) {

test/instrumentation/express-utils.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,26 @@ test('#getPathFromRequest', function (t) {
2626
t.equals(path, '/foo/bar');
2727
t.end();
2828
});
29+
t.test('should return path for express.use base urls as path ', function (t) {
30+
const req = createRequest(
31+
'https://test.com/foo/bar?query=value#hash',
32+
'example.com',
33+
{
34+
baseUrl: '/foo/bar',
35+
},
36+
);
37+
const path = getPathFromRequest(req, false, false);
38+
t.equals(path, '/foo/bar');
39+
t.end();
40+
});
2941
});
3042

31-
function createRequest(url, host = 'example.com') {
43+
function createRequest(url, host = 'example.com', additionalRequestItems) {
3244
return {
3345
url,
3446
headers: {
3547
host,
3648
},
49+
...additionalRequestItems,
3750
};
3851
}

0 commit comments

Comments
 (0)