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
JavaScript
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;