feat: make server fetch from localhost:3000

This commit is contained in:
complex 2025-04-11 14:49:19 +02:00
parent ba9912d5ad
commit deb4e728ab

@ -841,78 +841,47 @@ export class NostrHttpServer {
* @param request The HTTP request to execute
*/
private async executeHttpRequest(request: HttpRequest): Promise<string> {
// Check if the request is for a file in the public directory
const publicDir = path.join(process.cwd(), 'public');
const requestedPath = path.join(publicDir, request.path.replace(/^\//, ''));
// log the requested path
console.log('requestedPath', requestedPath);
console.log('publicDir', publicDir);
try {
// Check if the file exists and is within the public directory
if (fs.existsSync(requestedPath)) {
const filePath = publicDir + request.path;
// Make a request to localhost:3000
const response = await fetch(`http://localhost:3000${request.path}`, {
method: request.method,
headers: {
'Content-Type': 'application/json',
// Add any other headers from the original request if needed
},
// Add body if the request method is POST, PUT, etc.
...(request.method !== 'GET' && request.body ? { body: request.body } : {})
});
const fileContent = fs.readFileSync(filePath, 'utf8');
const contentType = this.getContentType(filePath);
// Get the response content
const responseContent = await response.text();
// Build response string with file content
return `HTTP/1.1 200 OK
Content-Type: ${contentType}
Content-Length: ${fileContent.length}
// Get all response headers
const headers = Array.from(response.headers.entries())
.map(([key, value]) => `${key}: ${value}`)
.join('\n');
${fileContent}`;
}
// Build response string with the content from localhost:3000
return `HTTP/1.1 ${response.status} ${response.statusText}
${headers}
Content-Length: ${responseContent.length}
${responseContent}`;
} catch (error) {
console.error('Error reading file:', error);
// If there's an error reading the file, fall back to the default response
}
console.error('Error making request to localhost:3000:', error);
// Return the default response if no file is found or there's an error
const responseBody = `
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
h1 {
color: #333;
}
.info {
background-color: #f5f5f5;
padding: 15px;
border-radius: 5px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Hello World from COMPLEX!</h1>
<p>This is a dummy response from the Nostr HTTP server.</p>
<div class="info">
<h2>Request Information:</h2>
<p><strong>Method:</strong> ${request.method}</p>
<p><strong>Path:</strong> ${request.path}</p>
<p><strong>Protocol:</strong> ${request.protocol}</p>
<p><strong>Timestamp:</strong> ${new Date().toISOString()}</p>
</div>
</body>
</html>`;
// Return an error response if the request fails
const errorBody = JSON.stringify({
error: 'Failed to connect to localhost:3000',
message: error instanceof Error ? error.message : 'Unknown error'
});
// Build response string
return `HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: ${responseBody.length}
return `HTTP/1.1 502 Bad Gateway
Content-Type: application/json
Content-Length: ${errorBody.length}
${responseBody}`;
${errorBody}`;
}
}
/**