Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created August 5, 2025 12:46
Show Gist options
  • Save tluyben/0e7beaa729047351abfd8aa96617e1fb to your computer and use it in GitHub Desktop.
Save tluyben/0e7beaa729047351abfd8aa96617e1fb to your computer and use it in GitHub Desktop.
little server to dump whatever is sent to it ; put in a dir, run node dump_server.js [port]
#!/usr/bin/env node
/**
* Test Dump Server
*
* Simple HTTP server that listens for POST requests and dumps the content to console.
* Useful for testing webhook functionality.
*
* Usage: node test_dump_server.js [port]
* Default port: 3000
*/
const http = require('http');
// Get port from command line or use default
const port = process.argv[2] ? parseInt(process.argv[2]) : 3000;
const server = http.createServer((req, res) => {
const timestamp = new Date().toISOString();
console.log('\n' + '='.repeat(80));
console.log(`[${timestamp}] Incoming ${req.method} request to ${req.url}`);
console.log('Headers:', JSON.stringify(req.headers, null, 2));
console.log('-'.repeat(80));
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log('POST Body:');
// Try to pretty-print JSON, fall back to raw body
try {
const jsonBody = JSON.parse(body);
console.log(JSON.stringify(jsonBody, null, 2));
// Show specific hook details if it's a hook payload
if (jsonBody.hook_type) {
console.log('\nπŸ“§ HOOK DETAILS:');
console.log(` Type: ${jsonBody.hook_type}`);
console.log(` Timestamp: ${jsonBody.timestamp}`);
if (jsonBody.metadata) {
console.log(' Metadata:');
Object.entries(jsonBody.metadata).forEach(([key, value]) => {
if (value !== null && value !== undefined) {
console.log(` ${key}: ${value}`);
}
});
}
if (jsonBody.parsed_email) {
console.log(' Email:');
console.log(` From: ${jsonBody.parsed_email.from?.text || jsonBody.parsed_email.from?.value?.[0]?.address || 'N/A'}`);
console.log(` To: ${jsonBody.parsed_email.to?.text || jsonBody.parsed_email.to?.value?.map(t => t.address).join(', ') || 'N/A'}`);
console.log(` Subject: ${jsonBody.parsed_email.subject || 'N/A'}`);
if (jsonBody.parsed_email.attachments && jsonBody.parsed_email.attachments.length > 0) {
console.log(` Attachments: ${jsonBody.parsed_email.attachments.length}`);
}
}
if (jsonBody.raw_email) {
console.log(` Raw Email Length: ${jsonBody.raw_email.length} characters`);
// Show first few lines of raw email
const rawLines = jsonBody.raw_email.split('\n').slice(0, 5);
console.log(' Raw Email Preview:');
rawLines.forEach(line => console.log(` ${line}`));
if (jsonBody.raw_email.split('\n').length > 5) {
console.log(' ... (truncated)');
}
}
}
} catch (e) {
console.log('Raw Body (not JSON):');
console.log(body);
}
console.log('='.repeat(80));
// Send success response
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
});
res.end(JSON.stringify({
status: 'success',
message: 'Webhook received and logged',
timestamp: timestamp,
receivedBytes: body.length
}));
});
} else if (req.method === 'GET') {
// Simple status endpoint
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'ok',
message: 'Test dump server is running',
port: port,
timestamp: timestamp
}));
} else if (req.method === 'OPTIONS') {
// Handle CORS preflight
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
});
res.end();
} else {
res.writeHead(405, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
error: 'Method not allowed',
message: `${req.method} is not supported. Use POST to send webhooks or GET to check status.`
}));
}
});
server.listen(port, () => {
console.log(`πŸ”— Test Dump Server listening on port ${port}`);
console.log(`πŸ“‘ Send webhooks to: http://localhost:${port}/webhook`);
console.log(`πŸ₯ Health check: http://localhost:${port}/`);
console.log('πŸ“ All POST requests will be dumped to console\n');
console.log('Press Ctrl+C to stop the server');
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\nπŸ‘‹ Shutting down test dump server...');
server.close(() => {
console.log('βœ… Server closed');
process.exit(0);
});
});
process.on('SIGTERM', () => {
console.log('\nπŸ‘‹ Received SIGTERM, shutting down...');
server.close(() => {
console.log('βœ… Server closed');
process.exit(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment