-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 1.99 KB
/
Copy pathserver.js
File metadata and controls
62 lines (55 loc) · 1.99 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
57
58
59
60
61
62
import express from 'express';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import path from 'path';
import cors from 'cors';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Explicitly load .env from project root (same directory as server.js)
dotenv.config({ path: path.join(__dirname, '.env') });
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static(__dirname));
const TAVILY_API_KEY = process.env.TAVILY_API_KEY;
if (!TAVILY_API_KEY) {
console.warn('Warning: TAVILY_API_KEY not set. Create a .env file based on .env.example');
} else {
console.log('Tavily key loaded (length):', TAVILY_API_KEY.length);
}
app.get('/api/health', (req, res) => {
res.json({ ok: true, hasKey: !!TAVILY_API_KEY });
});
app.post('/api/search', async (req, res) => {
try {
if (!TAVILY_API_KEY) {
return res.status(500).json({ detail: 'Server missing Tavily API key configuration.' });
}
const response = await fetch('https://api.tavily.com/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': TAVILY_API_KEY,
'Authorization': `Bearer ${TAVILY_API_KEY}`
},
body: JSON.stringify(req.body)
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
console.error('Tavily API error status:', response.status, data);
return res.status(response.status).json(data);
}
res.json(data);
} catch (err) {
console.error('Unexpected server error:', err);
res.status(500).json({ detail: 'Internal server error' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});