From d3d4da763dc33cd005099d49c4c5476a5f4a9418 Mon Sep 17 00:00:00 2001 From: zzlwte Date: Thu, 12 Dec 2024 22:06:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 ++ .env.production | 2 ++ nuxt.config.js | 24 ++++-------------- serverMiddleware/index.js | 18 ------------- serverMiddleware/logger.js | 52 -------------------------------------- 5 files changed, 9 insertions(+), 89 deletions(-) create mode 100644 .env.development create mode 100644 .env.production delete mode 100644 serverMiddleware/index.js delete mode 100644 serverMiddleware/logger.js diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..1a01d49 --- /dev/null +++ b/.env.development @@ -0,0 +1,2 @@ +API_DOMAIN=api.jiawen.live +RESOURCES=resources.jiawen.live \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..fdca822 --- /dev/null +++ b/.env.production @@ -0,0 +1,2 @@ +API_DOMAIN=${API_DOMAIN} +RESOURCES=${RESOURCES} \ No newline at end of file diff --git a/nuxt.config.js b/nuxt.config.js index 02b75bc..84c8ab1 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -24,15 +24,15 @@ module.exports = { ], link: [ { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }, - { rel: 'dns-prefetch', href: '//resources.jiawen.live' }, - { rel: 'dns-prefetch', href: '//api.jiawen.live' }, - { rel: 'stylesheet', href: 'https://cdn.bootcss.com/aplayer/1.10.1/APlayer.min.css'} + { rel: 'dns-prefetch', href: `//${process.env.RESOURCES}` }, + { rel: 'dns-prefetch', href: `//${process.env.API_DOMAIN}` }, + { rel: 'stylesheet', href: 'https://cdn.bootcss.com/aplayer/1.10.1/APlayer.min.css'} ], script: [ { async: 'async', type: 'text/javascript', - src: 'https://resources.jiawen.live/intersection-polyfill.js' + src: `https://${process.env.RESOURCES}/intersection-polyfill.js` }, { async: 'async', @@ -135,19 +135,5 @@ module.exports = { styleResources: { scss: ['./assets/scss/variables.scss', './assets/scss/mixin.scss'] - }, - /* - ** Build configuration - */ - build: { - /* - ** You can extend webpack config here - */ - extend(config, ctx) { - } - }, - serverMiddleware: [ - // 将日志中间件添加到 Nuxt.js 中间件中 - { path: '/api', handler: '~/serverMiddleware/logger.js' }, - ], + } } diff --git a/serverMiddleware/index.js b/serverMiddleware/index.js deleted file mode 100644 index 4a109c5..0000000 --- a/serverMiddleware/index.js +++ /dev/null @@ -1,18 +0,0 @@ -// serverMiddleware/logger.js -const logger = require('./logger'); - -// 使用 logger 记录日志 -export default function (req, res, next) { - logger.info('Request received', { - url: req.url, - method: req.method, - ip: req.ip - }); - - res.on('finish', () => { - // 记录响应状态码 - logger.info('Response sent', { statusCode: res.statusCode }); - }); - - next(); -} diff --git a/serverMiddleware/logger.js b/serverMiddleware/logger.js deleted file mode 100644 index a738329..0000000 --- a/serverMiddleware/logger.js +++ /dev/null @@ -1,52 +0,0 @@ -// npm install winston - -const winston = require('winston'); - -// 获取环境变量 -const isProduction = process.env.NODE_ENV === 'production'; -const logDirectory = 'logs'; // 日志存储目录 - -// 确保日志目录存在 -const fs = require('fs'); -const path = require('path'); -if (!fs.existsSync(logDirectory)) { - fs.mkdirSync(logDirectory); -} - -const transports = [ - // 文件输出:保存应用日志到文件 - new winston.transports.File({ - filename: path.join(logDirectory, 'app.log'), - format: winston.format.combine( - winston.format.timestamp(), // 添加时间戳 - winston.format.json() // 输出为 JSON 格式 - ), - level: 'info', - }), - ]; - - if (!isProduction) { - transports.push( - // 控制台输出,生产环境加颜色,开发环境详细日志 - new winston.transports.Console({ - format: winston.format.combine( - winston.format.colorize(), // 控制台输出日志上色 - winston.format.printf(({ timestamp, level, message, stack }) => { - return `${timestamp} ${level}: ${message} ${stack || ''}`; // 自定义控制台输出格式 - }) - ), - level: 'debug', // 根据环境设置日志级别 - }), - ) - } - -// 创建日志记录器 -const logger = winston.createLogger({ transports }); - -// 示例日志记录 -logger.info('Application started'); -logger.warn('This is a warning'); -logger.error('This is an error log'); -logger.debug('This is a debug message'); // 仅在开发环境下会输出 - -module.exports = logger;