route.ts 903 B

1234567891011121314151617181920212223242526272829
  1. import { NextResponse } from 'next/server'
  2. import mysql from 'mysql2/promise'
  3. // 创建全局的 MySQL 连接池
  4. const pool = mysql.createPool({
  5. connectionLimit: 10,
  6. host: '192.168.3.89', // 服务器地址
  7. user: 'root',
  8. password: '123456', // 密码
  9. database: 'chat',
  10. })
  11. export async function GET(request:any) {
  12. try {
  13. // 从连接池中获取连接
  14. const connection = await pool.getConnection()
  15. // 执行 MySQL 查询
  16. const [rows, fields] = await connection.query('SELECT he.id,he.dialog_name,de.id did,de.dialog_id,de.type,de.content FROM dialog he left join dialog_detail de on he.id = de.dialog_id')
  17. // 释放连接回连接池
  18. connection.release()
  19. return NextResponse.json({ data: rows }, { status: 200 })
  20. } catch (error) {
  21. console.error('Error:', error)
  22. return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
  23. }
  24. }