-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
136 lines (111 loc) · 3.85 KB
/
server.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import express from 'express';
// No longer using proxy middleware
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import open from 'open';
// Load environment variables from .env file
dotenv.config();
// Get __dirname equivalent in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = parseInt(process.env.PORT || '3000', 10);
const host = process.env.HOST || '0.0.0.0';
const loglevel = process.env.LOG_LEVEL || 'info';
// Define API target and optional key
const firecrawlApiUrl = process.env.FIRECRAWL_API_URL || 'http://firecrawl:3002';
const firecrawlApiKey = process.env.FIRECRAWL_API_KEY || '';
// Enable CORS for all routes
app.use(cors());
// Serve static files
app.use(express.static(path.join(__dirname, '.')));
// Create a simple proxy handler function
const proxyHandler = async (req, res) => {
try {
// Remove /api prefix from the path
const targetPath = req.url.replace(/^\/api/, '');
const targetUrl = `${firecrawlApiUrl}${targetPath}`;
console.log(`Proxying ${req.method} ${req.path} to ${targetUrl}`);
// Create options for the proxy request
const options = {
method: req.method,
headers: { ...req.headers }
};
// Add API key if provided
if (firecrawlApiKey) {
options.headers['Authorization'] = `Bearer ${firecrawlApiKey}`;
}
// Remove host header to avoid conflicts
delete options.headers.host;
// Handle request body for POST/PUT requests
let body = null;
if (req.method === 'POST' || req.method === 'PUT') {
// Parse request body
body = await new Promise((resolve) => {
const chunks = [];
req.on('data', (chunk) => chunks.push(chunk));
req.on('end', () => {
const bodyData = Buffer.concat(chunks).toString();
resolve(bodyData);
});
});
// Add body to request options
if (body) {
options.body = body;
// Ensure content-type is set
if (!options.headers['content-type']) {
options.headers['content-type'] = 'application/json';
}
}
}
// Use native fetch for proxying
const proxyRes = await fetch(targetUrl, options);
// Copy status and headers
res.statusCode = proxyRes.status;
// Copy headers
proxyRes.headers.forEach((value, key) => {
res.setHeader(key, value);
});
// Get response body
const responseBody = await proxyRes.text();
// Send response
res.end(responseBody);
} catch (err) {
console.error('Proxy error:', err);
res.writeHead(500, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({ error: 'Proxy error', message: err.message }));
}
};
// Use the proxy handler for all /api routes
app.use('/api', proxyHandler);
// Endpoint to get API configuration
app.get('/config', (req, res) => {
res.json({
apiEndpoint: firecrawlApiUrl,
apiKey: firecrawlApiKey ? true : false // Only send whether a key is configured, not the actual key
});
});
// Fallback route for SPA
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Start the server
app.listen(port, host, () => {
console.log(`Proxying API requests to ${firecrawlApiUrl}`);
console.log(`Server listening on ${host}:${port}`);
// log the url and port we're running on so the user can click it
const appUrl = `http://localhost:${port}`;
console.log(`Application started: ${appUrl}`);
// Open browser unless OPEN_BROWSER is set to 'false'
const shouldOpenBrowser = process.env.OPEN_BROWSER !== 'false';
if (shouldOpenBrowser) {
open(appUrl).catch(err => {
console.error('Failed to open browser:', err);
});
console.log('Browser opened automatically. Set OPEN_BROWSER=false to disable this behaviour.');
}
});