fix includePath in combination with router

This commit is contained in:
Konstantin Pogorelov
2016-11-28 11:28:05 +01:00
parent 08d98b450c
commit b374e7f54c
4 changed files with 16 additions and 8 deletions

View File

@@ -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

View File

@@ -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": [

View File

@@ -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");
});
});

View File

@@ -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);
};