Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (64 loc) · 2.34 KB
/
Copy pathserver.js
File metadata and controls
79 lines (64 loc) · 2.34 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// ============================================
// Serves the static site AND runs the /api/analyze
// relay to Claude, reading your key from .env.
//
// SETUP:
// npm install express dotenv
// node server.js
// Then open http://localhost:3000 in your browser.
// ============================================
require('dotenv').config();
const express = require('express');
const path = require('path');
const dns = require('dns');
// Some systems have broken/blocked IPv6 routing, which causes Node's
// native fetch to hang and time out even though the network is fine.
// Forcing IPv4 first works around this.
dns.setDefaultResultOrder('ipv4first');
const app = express();
const PORT = process.env.PORT || 3000;
console.log('Working directory:', process.cwd());
console.log('API key present:', !!process.env.ANTHROPIC_API_KEY);
app.use(express.json({ limit: '1mb' }));
app.use((req, res, next) => {
console.log(`Incoming: ${req.method} ${req.url}`);
next();
});
app.use(express.static(__dirname)); // serves index.html, css/, js/ as-is
app.post('/api/analyze', async (req, res) => {
console.log('Reached /api/analyze handler');
const { system, messages } = req.body;
if (!system || !messages) {
console.log('Missing system or messages in request body');
return res.status(400).json({ error: 'Missing system prompt or messages' });
}
try {
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01'
},
body: JSON.stringify({
model: 'claude-sonnet-4-6',
max_tokens: 6000,
system: system,
messages: messages
})
});
const data = await response.json();
if (!response.ok) {
console.log('Anthropic API returned an error:', JSON.stringify(data));
return res.status(response.status).json({ error: data?.error?.message || 'API error' });
}
console.log('Success — analysis returned.');
return res.status(200).json(data);
} catch (err) {
console.log('Caught exception in /api/analyze:', err);
return res.status(500).json({ error: err.message || 'Server error' });
}
});
app.listen(PORT, () => {
console.log(`PrepPath running locally at http://localhost:${PORT}`);
});