express-raw

1.2.5 • Public • Published

express-raw

Advanced Express.js Utilities for Modern Applications

npm version downloads GitHub stars

Zero-dependency toolkit for request analytics, performance monitoring, rate limiting, and real-time communication


✨ Features

Core Features

  • 🔍 Request Analytics
  • 🚦 Rate Limiting
  • 📊 Enhanced Logging
  • 👁️ DevTools Detection

Advanced Features

  • 🔌 WebSocket Support
  • 🎯 GraphQL Integration
  • 📈 Metrics Dashboard
  • 🔒 Security Suite

📦 Installation

npm install express-raw
Requirements
  • Node.js ≥ 14
  • Express.js ≥ 4

🚀 Quick Start

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

📚 Documentation

Rate Limiting

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
    }
});

Enhanced Logging

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
    }
});

Output Examples

# 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

WebSocket Support

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() }});

GraphQL Integration

const profiler = new GraphQLProfiler({
    slowQueryThreshold: 500,    // ms
    maxQueryComplexity: 100,
    maxDepth: 10,
    trackMemory: true
});

app.use('/graphql', profiler.middleware(logger));

Metrics Dashboard

const dashboard = new MetricsDashboard({
    updateInterval: 5000,
    enableRealtime: true,
    alerts: {
        maxMemoryUsage: 85,     // %
        maxErrorRate: 3         // %
    }
});

🎯 Examples

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

📋 Best Practices

Rate Limiting

  • Use sliding windows
  • Set route-specific limits
  • Enable auto-ban for security
  • Whitelist trusted IPs

WebSocket

  • Enable heartbeat
  • Implement authentication
  • Set connection limits
  • Handle reconnection

GraphQL

  • Set complexity limits
  • Monitor slow queries
  • Implement depth limiting
  • Cache common queries

Dashboard

  • Set alert thresholds
  • Monitor memory trends
  • Keep reasonable retention
  • Adjust update frequency

🔧 Troubleshooting

Common Issues & Solutions

Rate Limiter

// 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
    }
});

WebSocket

// Fix: Connection drops
const wsSupport = new WebSocketSupport({
    heartbeatInterval: 15000
});

// Fix: Memory leaks
const dashboard = new MetricsDashboard({
    retentionPeriod: 3600000,
    cleanup: true
});

📫 Support

Need help? Found a bug? Have a feature request?


Made with ♥ by ZeX

Readme

Keywords

Package Sidebar

Install

npm i express-raw

Weekly Downloads

9

Version

1.2.5

License

MIT

Unpacked Size

62.4 kB

Total Files

13

Last publish

Collaborators

  • ddosnotification