UNPKG

triostack-audit-sdk

Version:

A server-side audit logging middleware for Node.js applications with Express, Fastify, and Koa support. Includes comprehensive test examples and documentation.

73 lines (60 loc) • 2.15 kB
import express from 'express'; const app = express(); const PORT = 3002; // Store audit events in memory for testing const auditEvents = []; app.use(express.json()); // Endpoint to receive audit events app.post('/audit', (req, res) => { const auditEvent = req.body; const timestamp = new Date().toLocaleString(); // Store the event auditEvents.push({ ...auditEvent, receivedAt: timestamp }); // Log the event console.log('\nšŸ“Š AUDIT EVENT RECEIVED:'); console.log('ā° Time:', timestamp); console.log('šŸ‘¤ User ID:', auditEvent.userId); console.log('šŸ›£ļø Route:', auditEvent.route); console.log('šŸ“ Method:', auditEvent.method); console.log('ā±ļø Duration:', auditEvent.duration + 's'); console.log('šŸŒ Location:', `${auditEvent.city}, ${auditEvent.country}`); console.log('šŸ“± User Agent:', auditEvent.userAgent?.substring(0, 50) + '...'); console.log('šŸ“Š Status Code:', auditEvent.statusCode); console.log('šŸ“¦ Request Size:', auditEvent.requestSize + ' bytes'); console.log('šŸ“¦ Response Size:', auditEvent.responseSize + ' bytes'); console.log('šŸ“ IP:', auditEvent.ip); console.log('šŸŽÆ Session ID:', auditEvent.sessionId); console.log('─'.repeat(50)); res.json({ success: true, message: 'Audit event logged' }); }); // Endpoint to view all audit events app.get('/audit', (req, res) => { res.json({ totalEvents: auditEvents.length, events: auditEvents }); }); // Endpoint to clear audit events app.delete('/audit', (req, res) => { auditEvents.length = 0; res.json({ success: true, message: 'All audit events cleared' }); }); // Health check app.get('/health', (req, res) => { res.json({ status: 'healthy', eventsReceived: auditEvents.length, uptime: process.uptime() }); }); app.listen(PORT, () => { console.log(`šŸ“Š Mock audit server running on http://localhost:${PORT}`); console.log('šŸ“ POST /audit - Receive audit events'); console.log('šŸ‘€ GET /audit - View all events'); console.log('šŸ—‘ļø DELETE /audit - Clear all events'); console.log('ā¤ļø GET /health - Health check'); }); export default app;