From bad3fbe3cedf93baa0ae365cd7b9ce701b5c1991 Mon Sep 17 00:00:00 2001 From: wood Date: Wed, 17 Jul 2024 14:41:02 +0800 Subject: [PATCH] =?UTF-8?q?nodejs=E7=9A=84=E6=96=B9=E5=BC=8F=E9=87=8C?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=AF=B7=E6=B1=82=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodejs/app.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/nodejs/app.js b/nodejs/app.js index e50f981..6aa6a25 100644 --- a/nodejs/app.js +++ b/nodejs/app.js @@ -6,6 +6,36 @@ const port = 5003; // 外部 JSON 文件的 URL const CSV_PATHS_URL = 'https://random-api-file.czl.net/url.json'; +// 增强的日志中间件 +app.use((req, res, next) => { + const ip = req.headers['x-forwarded-for'] || req.ip || req.connection.remoteAddress; + const method = req.method; + const path = req.originalUrl; + const userAgent = req.get('User-Agent'); + const referer = req.get('Referer') || 'N/A'; + const accept = req.get('Accept'); + const acceptEncoding = req.get('Accept-Encoding'); + const acceptLanguage = req.get('Accept-Language'); + const host = req.get('Host'); + const protocol = req.protocol; + + console.log(` +[${new Date().toISOString()}] +IP: ${ip} +Method: ${method} +Path: ${path} +Protocol: ${protocol} +Host: ${host} +User-Agent: ${userAgent} +Referer: ${referer} +Accept: ${accept} +Accept-Encoding: ${acceptEncoding} +Accept-Language: ${acceptLanguage} + `); + + next(); +}); + /** * 处理客户端请求,并根据请求的URL路径获取对应的CSV文件中的随机一行的URL,然后重定向到该URL。 */