From 4709de3213b4ed3f58b37e361cad4443f031d911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Tue, 30 Dec 2025 11:09:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86server-example=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=9A=E7=A7=BB=E9=99=A4=E8=B0=83=E8=AF=95=E5=92=8C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-example/free-port.js | 87 ------------------------------------ server-example/list-users.js | 67 --------------------------- 2 files changed, 154 deletions(-) delete mode 100644 server-example/free-port.js delete mode 100644 server-example/list-users.js diff --git a/server-example/free-port.js b/server-example/free-port.js deleted file mode 100644 index e01b116..0000000 --- a/server-example/free-port.js +++ /dev/null @@ -1,87 +0,0 @@ -// 检查并释放被占用的端口 -const { exec } = require('child_process'); -const os = require('os'); - -// 要检查的端口 -const PORT = 3001; - -function killProcessOnPort(port) { - console.log(`开始检查端口 ${port} 的占用情况...`); - - if (os.platform() === 'win32') { - // Windows系统 - exec(`netstat -ano | findstr :${port}`, (error, stdout) => { - if (error) { - console.log(`端口 ${port} 未被占用`); - return; - } - - const lines = stdout.trim().split('\n'); - if (lines.length > 0) { - // 提取PID - const pid = lines[0].trim().split(/\s+/).pop(); - console.log(`发现进程 ${pid} 占用端口 ${port}`); - - // 杀死进程 - exec(`taskkill /F /PID ${pid}`, (killError) => { - if (killError) { - console.error(`杀死进程 ${pid} 失败:`, killError.message); - } else { - console.log(`成功杀死进程 ${pid},端口 ${port} 已释放`); - } - }); - } else { - console.log(`端口 ${port} 未被占用`); - } - }); - } else { - // Linux/Mac系统 - exec(`lsof -i :${port}`, (error, stdout) => { - if (error) { - console.log(`端口 ${port} 未被占用`); - return; - } - - const lines = stdout.trim().split('\n'); - if (lines.length > 1) { - // 提取PID - const pid = lines[1].trim().split(/\s+/)[1]; - console.log(`发现进程 ${pid} 占用端口 ${port}`); - - // 杀死进程 - exec(`kill -9 ${pid}`, (killError) => { - if (killError) { - console.error(`杀死进程 ${pid} 失败:`, killError.message); - } else { - console.log(`成功杀死进程 ${pid},端口 ${port} 已释放`); - } - }); - } else { - console.log(`端口 ${port} 未被占用`); - } - }); - } -} - -// 执行端口检查和释放 -killProcessOnPort(PORT); - -// 延迟2秒后再次启动服务器(如果是Windows系统) -setTimeout(() => { - if (os.platform() === 'win32') { - console.log('\n正在尝试重新启动服务器...'); - const serverProcess = exec('node server-mysql.js'); - - serverProcess.stdout.on('data', (data) => { - console.log(`服务器输出: ${data}`); - }); - - serverProcess.stderr.on('data', (data) => { - console.error(`服务器错误: ${data}`); - }); - - serverProcess.on('close', (code) => { - console.log(`服务器进程退出,代码: ${code}`); - }); - } -}, 2000); \ No newline at end of file diff --git a/server-example/list-users.js b/server-example/list-users.js deleted file mode 100644 index 7a1a054..0000000 --- a/server-example/list-users.js +++ /dev/null @@ -1,67 +0,0 @@ -require('dotenv').config(); -const mysql = require('mysql2/promise'); - -// 查询用户信息 -async function listUsers() { - let connection = null; - try { - console.log('连接数据库...'); - connection = await mysql.createConnection({ - host: process.env.DB_HOST || 'localhost', - user: process.env.DB_USER || 'root', - password: process.env.DB_PASSWORD === undefined ? null : process.env.DB_PASSWORD, - database: process.env.DB_DATABASE || 'wechat_app', - port: process.env.DB_PORT || 3306, - timezone: '+08:00' // 设置时区为UTC+8 - }); - console.log('数据库连接成功\n'); - - // 查询users表结构 - console.log('=== users表结构 ==='); - const [columns] = await connection.execute( - 'SHOW COLUMNS FROM users' - ); - console.log('字段列表:', columns.map(col => col.Field).join(', ')); - console.log(); - - // 查询前5个用户数据 - console.log('=== 用户数据 (前5个) ==='); - const [users] = await connection.execute( - 'SELECT * FROM users LIMIT 5' - ); - console.log(`数据库中有 ${users.length} 个用户记录`); - - if (users.length > 0) { - users.forEach((user, index) => { - console.log(`${index + 1}. 用户数据:`, user); - }); - - // 查找可能的openid字段 - const firstUser = users[0]; - const possibleOpenIdFields = Object.keys(firstUser).filter(key => - key.toLowerCase().includes('openid') || key.toLowerCase().includes('open_id') - ); - - if (possibleOpenIdFields.length > 0) { - console.log('\n=== 可能的openid字段 ==='); - console.log('找到以下可能的openid字段:', possibleOpenIdFields.join(', ')); - console.log('请使用其中一个有效的字段更新测试脚本'); - } else { - console.log('\n未找到明显的openid字段,请检查users表结构后手动更新测试脚本'); - } - } else { - console.log('\n数据库中没有用户记录'); - } - - } catch (error) { - console.error('查询过程中发生错误:', error); - } finally { - if (connection) { - await connection.end(); - console.log('\n数据库连接已关闭'); - } - } -} - -// 运行查询 -listUsers(); \ No newline at end of file