Full-Stack Project Development

Build a complete full-stack application from scratch including frontend, backend, database, authentication, and deployment.

advanced Backend Development 10 hours

Chapter 15: Production Maintenance

Chapter 15 of 15

Chapter 15: Production Maintenance

15.1 Monitoring and Updates

Ongoing monitoring and maintenance ensure applications run smoothly in production.

// Application monitoring
const prometheus = require('prom-client');

// Collect default metrics
const register = new prometheus.Registry();
prometheus.collectDefaultMetrics({ register });

// Custom metrics
const httpRequestDuration = new prometheus.Histogram({
    name: 'http_request_duration_seconds',
    help: 'Duration of HTTP requests in seconds',
    labelNames: ['method', 'route', 'status_code']
});

// Expose metrics endpoint
app.get('/metrics', async (req, res) => {
    res.set('Content-Type', register.contentType);
    res.end(await register.metrics());
});

15.2 Logging and Alerting

// Structured logging
const winston = require('winston');

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    transports: [
        new winston.transports.File({ filename: 'error.log', level: 'error' }),
        new winston.transports.File({ filename: 'combined.log' })
    ]
});

// Alert on errors
logger.on('error', (error) => {
    // Send alert to monitoring service
    sendAlert('Application error detected', error);
});

15.3 Backup and Recovery

// Database backup script
const { exec } = require('child_process');

function backupDatabase() {
    const backupFile = `backup-${Date.now()}.sql`;
    exec(`pg_dump mydb > ${backupFile}`, (error) => {
        if (error) {
            logger.error('Backup failed:', error);
        } else {
            logger.info('Backup created:', backupFile);
            // Upload to cloud storage
        }
    });
}

// Schedule daily backups
setInterval(backupDatabase, 24 * 60 * 60 * 1000);

15.4 Performance Monitoring

// Track response times
app.use((req, res, next) => {
    const start = Date.now();
    res.on('finish', () => {
        const duration = Date.now() - start;
        logger.info('Request completed', {
            method: req.method,
            path: req.path,
            status: res.statusCode,
            duration: `${duration}ms`
        });
    });
    next();
});

Conclusion

Master full-stack development by building complete projects from start to finish.