81 lines
2.2 KiB
JavaScript
81 lines
2.2 KiB
JavaScript
const http = require('http');
|
|
|
|
function parseArgs() {
|
|
const args = process.argv.slice(2);
|
|
const options = {
|
|
port: 3000,
|
|
host: 'localhost'
|
|
};
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
if (args[i] === '--port' || args[i] === '-p') {
|
|
options.port = parseInt(args[i + 1]) || 3000;
|
|
i++;
|
|
} else if (args[i] === '--host' || args[i] === '-h') {
|
|
options.host = args[i + 1] || 'localhost';
|
|
i++;
|
|
} else if (args[i] === '--help') {
|
|
console.log(`
|
|
Usage: node server.js [options]
|
|
|
|
Options:
|
|
--port, -p Port to run the server on (default: 3000)
|
|
--host, -h Host to bind the server to (default: localhost)
|
|
--help Show this help message
|
|
`);
|
|
process.exit(0);
|
|
}
|
|
}
|
|
return options;
|
|
}
|
|
|
|
const options = parseArgs();
|
|
|
|
const server = http.createServer((req, res) => {
|
|
let body = '';
|
|
|
|
console.log(`\n${new Date().toISOString()} - ${req.method} ${req.url}`);
|
|
console.log('Headers:', JSON.stringify(req.headers, null, 2));
|
|
|
|
req.on('data', chunk => {
|
|
body += chunk.toString();
|
|
});
|
|
|
|
req.on('end', () => {
|
|
if (body) {
|
|
try {
|
|
const jsonBody = JSON.parse(body);
|
|
console.log('Body:', JSON.stringify(jsonBody, null, 2));
|
|
} catch {
|
|
console.log('Body:', body);
|
|
}
|
|
}
|
|
|
|
res.writeHead(200, {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
|
|
});
|
|
|
|
const response = {
|
|
message: 'Request received',
|
|
method: req.method,
|
|
url: req.url,
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
res.end(JSON.stringify(response));
|
|
});
|
|
});
|
|
|
|
server.listen(options.port, options.host, () => {
|
|
console.log(`Webhook listener running on http://${options.host}:${options.port}`);
|
|
|
|
process.on('SIGINT', () => {
|
|
console.log('\nReceived CTRL+C - Shutting down server...');
|
|
server.close(() => {
|
|
console.log('Server closed');
|
|
process.exit(0);
|
|
});
|
|
});
|
|
});
|