Zero-dependency toolkit for request analytics, performance monitoring, rate limiting, and real-time communication
|
|
npm install express-raw
Requirements
- Node.js ≥ 14
- Express.js ≥ 4
const express = require('express');
const {
expressLogger,
RateLimiter,
WebSocketSupport,
MetricsDashboard
} = require('express-raw');
const app = express();
// Initialize
const logger = new expressLogger();
const limiter = new RateLimiter({ maxRequests: 100 });
const dashboard = new MetricsDashboard();
// Apply middleware
app.use(limiter.middleware(logger));
app.use(logger.middleware());
// Start server
app.listen(3000, () => logger.serverStart(3000));
Configuration Options
const limiter = new RateLimiter({
// Time Window
windowMs: 15 * 60 * 1000, // 15 minutes
maxRequests: 100,
windowType: 'sliding', // 'sliding' | 'fixed'
// Route Limits
routeLimits: {
'/api/auth/.*': 20, // Auth routes: 20 req/window
'/api/upload/.*': 10 // Upload routes: 10 req/window
},
// Security
autoBan: {
enabled: true,
maxViolations: 3, // Ban after 3 violations
banDurationMs: 24 * 60 * 60 * 1000 // 24h
}
});
Configuration & Examples
const logger = new expressLogger({
enabled: {
server: true, // Server logs
requests: true, // Request logs
responses: true, // Response logs
websocket: true, // WebSocket logs
graphql: true // GraphQL logs
}
});
# Server Start
[2024-11-25T19:38:20.177Z] ⚡ [SERVER] Server started
Port: 3000
Environment: development
Memory: 8MB
# Rate Limit Event
[2024-11-25T19:38:26.177Z] ⚠️ [RATELIMIT] Rate limit exceeded
IP: 192.168.1.100
Path: /api/users
ViolationCount: 1
const wsSupport = new WebSocketSupport({
heartbeatInterval: 30000,
rateLimiting: {
enabled: true,
maxConnectionsPerIP: 5
},
auth: {
enabled: true,
handler: async (req) => {
// Auth logic
}
}
});
// Broadcast
wsSupport.broadcast({ type: 'update', data: { time: Date.now() }});
const profiler = new GraphQLProfiler({
slowQueryThreshold: 500, // ms
maxQueryComplexity: 100,
maxDepth: 10,
trackMemory: true
});
app.use('/graphql', profiler.middleware(logger));
const dashboard = new MetricsDashboard({
updateInterval: 5000,
enableRealtime: true,
alerts: {
maxMemoryUsage: 85, // %
maxErrorRate: 3 // %
}
});
Complete Application Example
const express = require('express');
const {
expressLogger,
RateLimiter,
WebSocketSupport,
GraphQLProfiler,
MetricsDashboard
} = require('express-raw');
const app = express();
// Initialize components
const logger = new expressLogger({
enabled: {
rateLimit: true,
websocket: true,
graphql: true
}
});
const limiter = new RateLimiter({
windowMs: 15 * 60 * 1000,
maxRequests: 100,
autoBan: { enabled: true }
});
const wsSupport = new WebSocketSupport({
rateLimiting: { enabled: true }
});
const profiler = new GraphQLProfiler({
slowQueryThreshold: 500
});
const dashboard = new MetricsDashboard({
enableRealtime: true
});
// Apply middleware
app.use(limiter.middleware(logger));
app.use(logger.middleware());
app.use('/graphql', profiler.middleware(logger));
app.use(dashboard.middleware(logger, limiter, profiler));
// Start server
const server = app.listen(3000, () => {
logger.serverStart(3000);
});
wsSupport.middleware(logger)(server);
|
|
|
|
Common Issues & Solutions
// Fix: Too many false positives
const limiter = new RateLimiter({
windowType: 'sliding',
maxRequests: 200
});
// Fix: Auto-ban too aggressive
const limiter = new RateLimiter({
autoBan: {
maxViolations: 5,
banDurationMs: 60 * 60 * 1000
}
});
// Fix: Connection drops
const wsSupport = new WebSocketSupport({
heartbeatInterval: 15000
});
// Fix: Memory leaks
const dashboard = new MetricsDashboard({
retentionPeriod: 3600000,
cleanup: true
});
Need help? Found a bug? Have a feature request?
Made with ♥ by ZeX