# 光湖聊天前端 · 部署指南 ## 1. 服务器环境准备 SSH登录 `43.153.203.105`(这台服务器Node 20 + PM2已装·可跳过这步): ```bash # 安装 Node.js 20.x curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt-get install -y nodejs # 验证 node -v npm -v ``` ## 2. 创建项目 ```bash # 创建项目文件夹 mkdir -p /opt/guanghu-chat cd /opt/guanghu-chat # 初始化 npm init -y npm install express ``` ## 3. 后端代码 复制下面整块命令粘贴到终端回车(会自动创建 `server.js` 文件): ```bash cat > /opt/guanghu-chat/server.js << 'SERVEREOF' const express = require('express'); const path = require('path'); const app = express(); // ====== 配置区(全部填好·直接用)====== const CONFIG = { API_KEY: 'ARK_API_KEY_REDACTED', API_URL: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions', MODEL: 'qwen3-8b-ft-202604281809-9f30-64d09c149996', PORT: 3000 }; // ============================== app.use(express.json()); app.use(express.static(path.join(__dirname, 'public'))); // 聊天接口 app.post('/api/chat', async (req, res) => { try { const { messages } = req.body; // 纯 user/assistant 对话·不注入 system prompt·人格在权重里 const apiMessages = [...messages]; // 设置流式响应头 res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); const response = await fetch(CONFIG.API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${CONFIG.API_KEY}` }, body: JSON.stringify({ model: CONFIG.MODEL, messages: apiMessages, stream: true, temperature: 0.7, max_tokens: 2048 }) }); if (!response.ok) { const err = await response.text(); res.write(`data: {"error": "API错误: ${response.status}"} `); res.end(); return; } // 流式转发 const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value, { stream: true }); res.write(chunk); } res.end(); } catch (error) { console.error('Error:', error); res.status(500).json({ error: '服务器错误' }); } }); app.listen(CONFIG.PORT, '0.0.0.0', () => { console.log(`光湖聊天服务已启动: http://0.0.0.0:${CONFIG.PORT}`); }); ``` ## 4. 前端代码 ```bash mkdir -p /opt/guanghu-chat/public nano /opt/guanghu-chat/public/index.html ``` 粘贴以下内容: ```html