-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (47 loc) · 1.48 KB
/
Copy pathserver.js
File metadata and controls
56 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import express from 'express';
import { config } from 'dotenv';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
config({ path: '.env.local' });
config();
const app = express();
const port = process.env.PORT || 3000;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const distDir = path.join(__dirname, 'dist');
app.use(express.json({ limit: '10mb' }));
app.post('/api/deepseek/chat', async (req, res) => {
const apiKey = process.env.DEEPSEEK_API_KEY;
if (!apiKey) {
res.status(500).json({ error: { message: 'DEEPSEEK_API_KEY is not configured' } });
return;
}
try {
const upstream = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: process.env.DEEPSEEK_MODEL || 'deepseek-v4-flash',
...req.body,
}),
});
res.status(upstream.status);
res.setHeader('Content-Type', upstream.headers.get('content-type') || 'application/json');
res.send(await upstream.text());
} catch (error) {
res.status(500).json({
error: {
message: error?.message || 'DeepSeek request failed',
},
});
}
});
app.use(express.static(distDir));
app.get('*', (_req, res) => {
res.sendFile(path.join(distDir, 'index.html'));
});
app.listen(port, () => {
console.log(`DianQian demo server running on http://0.0.0.0:${port}`);
});