messages passed to middleware

This commit is contained in:
Mateo Körber Belmonte
2025-03-25 00:03:05 +01:00
parent a9bd5c4c04
commit a32fffe137
+39
View File
@@ -2,18 +2,57 @@ const fs = require('fs');
const https = require('https'); const https = require('https');
const express = require('express'); const express = require('express');
const cors = require('cors'); const cors = require('cors');
// Keep this import for the proxy functionality
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const app = express(); const app = express();
const port = 3000; const port = 3000;
// Enable CORS for all routes
app.use(cors()); app.use(cors());
// Serve static files
app.use('/static', express.static('public')); app.use('/static', express.static('public'));
// Add proxy endpoint for chat requests
app.post('/proxy/chat', express.json(), async (req, res) => {
try {
console.log('Proxying request to backend:', req.body);
const response = await fetch('http://localhost:4000/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(req.body)
});
if (!response.ok) {
console.error('Backend error:', response.status, response.statusText);
return res.status(response.status).json({
error: 'Backend server error',
status: response.status
});
}
const data = await response.json();
console.log('Backend response:', data);
res.json(data);
} catch (error) {
console.error('Proxy error:', error);
res.status(500).json({
error: 'Proxy request failed',
message: error.message
});
}
});
// HTTPS configuration
const options = { const options = {
key: fs.readFileSync('localhost-key.pem'), key: fs.readFileSync('localhost-key.pem'),
cert: fs.readFileSync('localhost.pem') cert: fs.readFileSync('localhost.pem')
}; };
// Start HTTPS server
https.createServer(options, app).listen(port, () => { https.createServer(options, app).listen(port, () => {
console.log(`✅ HTTPS-Server läuft auf https://localhost:${port}`); console.log(`✅ HTTPS-Server läuft auf https://localhost:${port}`);
console.log(`✅ Proxy-Endpoint verfügbar unter https://localhost:${port}/proxy/chat`);
}); });