-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (89 loc) · 2.88 KB
/
server.js
File metadata and controls
106 lines (89 loc) · 2.88 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const path = require('path');
const cors = require('cors');
const { createProxyMiddleware } = require('http-proxy-middleware');
const multer = require('multer');
const upload = multer({ storage: multer.memoryStorage() });
const app = express();
const PORT = process.env.PORT || 8002;
// Enable CORS
app.use(cors());
// Parse JSON bodies
app.use(express.json());
// Serve static files
app.use(express.static(path.join(__dirname)));
// Configure proxy middleware
const backendUrl = process.env.BACKEND_URL || '/tutor/api';
const apiProxy = createProxyMiddleware({
target: 'http://localhost:8080',
changeOrigin: true,
pathRewrite: {
'^/api': ''
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({ error: 'Backend server unavailable' });
}
});
// Handle audio processing specifically
app.post('/api/process_audio', upload.single('audio'), async (req, res) => {
console.log('Received audio processing request');
try {
// Forward the request to the backend
const forwardUrl = `http://localhost:8080/process_audio`;
// Create a FormData-like object for node-fetch
const FormData = require('form-data');
const form = new FormData();
// Add the audio file if it exists
if (req.file) {
form.append('audio', req.file.buffer, {
filename: 'recording.wav',
contentType: 'audio/wav'
});
}
// Add the data if it exists
if (req.body.data) {
form.append('data', req.body.data);
}
// Forward the request
const fetch = require('node-fetch');
const response = await fetch(forwardUrl, {
method: 'POST',
body: form,
headers: form.getHeaders()
});
if (!response.ok) {
const errorText = await response.text();
console.error(`Backend error: ${response.status} - ${errorText}`);
throw new Error(`Backend responded with status: ${response.status}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error forwarding request to backend:', error);
res.status(500).json({ error: 'Failed to process audio: ' + error.message });
}
});
// Use proxy for other API routes
app.use('/api', apiProxy);
// Also handle the /tutor/api route for direct access
app.use('/tutor/api', createProxyMiddleware({
target: 'http://localhost:8080',
changeOrigin: true,
pathRewrite: {
'^/tutor/api': ''
},
onError: (err, req, res) => {
console.error('Proxy error:', err);
res.status(500).json({ error: 'Backend server unavailable' });
}
}));
// Serve index.html for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`API requests will be proxied to ${backendUrl}`);
});