mirror of
https://github.com/BreizhHardware/express-prom-bundle.git
synced 2026-03-18 21:30:38 +01:00
fix includePath in combination with router
This commit is contained in:
@@ -116,7 +116,7 @@ app.listen(3000);
|
||||
|
||||
## Changelog
|
||||
|
||||
* **1.2.0**
|
||||
* **1.2.1**
|
||||
* upgrade prom-client to 6.1.2
|
||||
* add options: includeMethod, includePath, keepDefaultMetrics
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "express-prom-bundle",
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.1",
|
||||
"description": "express middleware with popular prometheus metrics in one bundle",
|
||||
"main": "src/index.js",
|
||||
"keywords": [
|
||||
|
||||
@@ -6,23 +6,23 @@ const normalizePath = require("../src/normalizePath");
|
||||
describe("normalizePath", () => {
|
||||
it("returns original if disabled in opts", () => {
|
||||
expect(
|
||||
normalizePath({path: "/a/12345"}, {normalizePath: false})
|
||||
normalizePath({originalUrl: "/a/12345"}, {normalizePath: false})
|
||||
).toBe("/a/12345");
|
||||
});
|
||||
|
||||
it("returns run callback if configured", () => {
|
||||
expect(
|
||||
normalizePath(
|
||||
{path: "/a/12345"},
|
||||
{originalUrl: "/a/12345"},
|
||||
{
|
||||
normalizePath: req => req.path + "-ok"
|
||||
normalizePath: req => req.originalUrl + "-ok"
|
||||
}
|
||||
)
|
||||
).toBe("/a/12345-ok");
|
||||
});
|
||||
|
||||
it("uses UrlValueParser by default", () => {
|
||||
expect(normalizePath({path: "/a/12345"}))
|
||||
expect(normalizePath({originalUrl: "/a/12345"}))
|
||||
.toBe("/a/#val");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
const UrlValueParser = require("url-value-parser");
|
||||
const url = require("url");
|
||||
let urlValueParser;
|
||||
|
||||
module.exports = function(req, opts) {
|
||||
opts = opts || {};
|
||||
|
||||
// originalUrl is taken, because url and path can be changed
|
||||
// by middlewares such as "router". Note: this function is called onFinish
|
||||
/// i.e. always in the tail of the middleware chain
|
||||
const path = url.parse(req.originalUrl).pathname;
|
||||
|
||||
if (opts.normalizePath !== undefined && !opts.normalizePath) {
|
||||
return req.path;
|
||||
return path;
|
||||
}
|
||||
if (typeof opts.normalizePath === "function") {
|
||||
return opts.normalizePath(req, opts);
|
||||
}
|
||||
|
||||
if (!urlValueParser) {
|
||||
urlValueParser = new UrlValueParser();
|
||||
}
|
||||
return urlValueParser.replacePathValues(req.path);
|
||||
return urlValueParser.replacePathValues(path);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user