fix: 埋点正则兼容/api前缀(originalUrl含前缀)

This commit is contained in:
Hermes Agent
2026-09-19 17:02:16 +08:00
parent 5f3809831f
commit ce9f69a76e
+3 -3
View File
@@ -68,12 +68,12 @@ app.use('/api/', limiter)
// 访问埋点:记录页面级 GET 请求(排除管理后台/认证/健康检查/高频轮询接口),小程序与 H5 访问均可统计 // 访问埋点:记录页面级 GET 请求(排除管理后台/认证/健康检查/高频轮询接口),小程序与 H5 访问均可统计
// PV 口径:只记"页面首屏"接口(每打开一页只计1次),轮询/统计类接口不计——避免一次访问刷出6-8条 // PV 口径:只记"页面首屏"接口(每打开一页只计1次),轮询/统计类接口不计——避免一次访问刷出6-8条
const TRACKABLE_PAGES = [/^\/knowledge(\?|$)/, /^\/trend(\?|$)/, /^\/knowledge\/stats/, /^\/ai-chat\/quota/, /^\/user\/leaderboard/, /^\/feedback/] const TRACKABLE_PAGES = [/^\/?api\/knowledge(\?|$)/, /^\/?api\/trend(\?|$)/, /^\/?api\/knowledge\/stats/, /^\/?api\/ai-chat\/quota/, /^\/?api\/user\/leaderboard/, /^\/?api\/feedback/]
app.use('/api', (req, res, next) => { app.use('/api', (req, res, next) => {
try { try {
if (req.method === 'GET' && !req.path.startsWith('/admin') && !req.path.startsWith('/auth')) { if (req.method === 'GET' && !req.path.startsWith('/admin') && !req.path.startsWith('/auth')) {
const path = String(req.originalUrl || req.path).split('?')[0] const fullPath = String(req.originalUrl || req.path).split('?')[0]
const isPage = TRACKABLE_PAGES.some((re) => re.test(path)) const isPage = TRACKABLE_PAGES.some((re) => re.test(fullPath)) || TRACKABLE_PAGES.some((re) => re.test(fullPath.replace(/^\/api/, '')))
if (isPage) { if (isPage) {
const ua = String(req.headers['user-agent'] || '') const ua = String(req.headers['user-agent'] || '')
const referer = String(req.headers.referer || '') const referer = String(req.headers.referer || '')