Skip to content

Instantly share code, notes, and snippets.

@greenido
Created July 29, 2025 20:37
Show Gist options
  • Select an option

  • Save greenido/8709df108a4679d57c90ce8210eb5806 to your computer and use it in GitHub Desktop.

Select an option

Save greenido/8709df108a4679d57c90ce8210eb5806 to your computer and use it in GitHub Desktop.
const http = require('http');
const url = require('url');
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Handle different routes
switch (parsedUrl.pathname) {
case '/':
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>EOS Tunnel Test Server</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
min-height: 100vh;
}
.container {
background: rgba(255,255,255,0.1);
padding: 30px;
border-radius: 15px;
backdrop-filter: blur(10px);
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.endpoint {
background: rgba(255,255,255,0.2);
padding: 15px;
margin: 10px 0;
border-radius: 8px;
border-left: 4px solid #fff;
}
.method {
font-weight: bold;
color: #ffd700;
}
.url {
font-family: monospace;
background: rgba(0,0,0,0.3);
padding: 5px;
border-radius: 4px;
}
.description {
margin-top: 10px;
font-style: italic;
}
.status {
text-align: center;
margin-top: 30px;
padding: 20px;
background: rgba(255,255,255,0.2);
border-radius: 8px;
}
.timestamp {
font-size: 0.9em;
opacity: 0.8;
}
</style>
</head>
<body>
<div class="container">
<h1>EOS Tunnel Test Server</h1>
<p>This is a test server to demonstrate the tunnel functionality. Try accessing different endpoints:</p>
<div class="endpoint">
<div class="method">GET</div>
<div class="url">/api/status</div>
<div class="description">Get server status and information</div>
</div>
<div class="endpoint">
<div class="method">GET</div>
<div class="url">/api/random</div>
<div class="description">Get a random number</div>
</div>
<div class="endpoint">
<div class="method">POST</div>
<div class="url">/api/echo</div>
<div class="description">Echo back the request body</div>
</div>
<div class="endpoint">
<div class="method">GET</div>
<div class="url">/api/time</div>
<div class="description">Get current server time</div>
</div>
<div class="status">
<h3>Server Status</h3>
<p>✅ Server is running on port 8080</p>
<p class="timestamp">Started at: ${new Date().toLocaleString()}</p>
</div>
</div>
</body>
</html>
`);
break;
case '/api/status':
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'running',
server: 'EOS Tunnel Test Server',
version: '1.0.0',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage(),
endpoints: [
{ method: 'GET', path: '/api/status', description: 'Server status' },
{ method: 'GET', path: '/api/random', description: 'Random number' },
{ method: 'POST', path: '/api/echo', description: 'Echo request body' },
{ method: 'GET', path: '/api/time', description: 'Current time' }
]
}));
break;
case '/api/random':
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
random: Math.floor(Math.random() * 1000),
timestamp: new Date().toISOString()
}));
break;
case '/api/echo':
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
method: req.method,
headers: req.headers,
body: body,
timestamp: new Date().toISOString()
}));
});
} else {
res.writeHead(405, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Method not allowed' }));
}
break;
case '/api/time':
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
time: new Date().toISOString(),
timezone: 'UTC',
timestamp: Date.now()
}));
break;
default:
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
error: 'Not found',
message: `Endpoint ${parsedUrl.pathname} does not exist`,
availableEndpoints: [
'/',
'/api/status',
'/api/random',
'/api/echo',
'/api/time'
]
}));
}
});
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
console.log(`🧪 Our wonderful Test server running on http://localhost:${PORT}`);
console.log(`📋 Available endpoints:`);
console.log(` GET / - Home page`);
console.log(` GET /api/status - Server status`);
console.log(` GET /api/random - Random number`);
console.log(` POST /api/echo - Echo request body`);
console.log(` GET /api/time - Current time`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment