Development Server Setup with PM2 and Hot Reloading

Overview

This document provides a comprehensive guide to setting up a robust development server with PM2 process management, hot reloading, and Playwright integration for the Waves and Algorithms project.

Key Features

Installation and Setup

1. Install Dependencies

# Install core dependencies
npm install

# Install Playwright browsers
npx playwright install

2. Development Server Configuration

The development server (dev-server.js) provides:

3. PM2 Configuration

The PM2 configuration (ecosystem.config.js) includes:

4. Playwright Integration

The test launcher (test-launcher.js) provides:

Usage

Development Mode

# Start development server with PM2
npm run dev:pm2

# Start development server directly
npm run dev

# Monitor server status
npm run pm2:monit

# View logs
npm run pm2:logs

Testing

# Run Playwright tests
npm run test:e2e

# Run tests with UI mode
npm run test:e2e:ui

# Update test snapshots
npm run test:e2e:update

PM2 Management

# Start server
npm run pm2:start

# Stop server
npm run pm2:stop

# Restart server
npm run pm2:restart

# Delete server
npm run pm2:delete

# Save current process list
npm run pm2:save

# Restore saved process list
npm run pm2:resurrect

Browser Automation

Automatic Chrome Launching

The system automatically launches Chrome when:

Preview Feature

When you complete a new feature on an HTML file and request a preview:

Smart Window Management

The system includes intelligent window management:

Development Workflow

  1. Start Server: npm run dev:pm2 - Chrome opens automatically
  2. Edit File: Make changes to any HTML/CSS/JS file
  3. Auto Refresh: Chrome automatically refreshes and comes to front
  4. See Changes: Immediate visual feedback without any manual intervention
  5. Continue Development: Make more changes and see updates instantly

Usage Examples

# Start development server - Chrome opens automatically
npm run dev:pm2

# Modify a file - Chrome refreshes and comes to front
echo "console.log('test');" >> public_html/script.js

# Request preview of specific file - Chrome opens and comes to front
curl -X POST http://localhost:8000/preview?file=index.html

# Edit HTML file - Chrome refreshes and brings window to front
echo "

New Feature

" >> public_html/index.html

API Reference

Development Server API

Endpoints

GET / - Serve main page

GET /health - Health check endpoint

GET /__hot-reload - Hot reload endpoint

GET /path/to/file - Serve static file

POST /preview?file= - Automatically open file in Chrome for preview

GET /preview - List available files for preview

WebSocket Events

PM2 API

Process Management

Monitoring

Playwright API

Test Execution

Configuration

Troubleshooting

Common Issues

1. Port Already in Use

# Check port usage
lsof -i :8000

# Kill process using port
kill -9 

2. PM2 Process Issues

# Reset PM2
npm run pm2:kill
npm run pm2:start

3. File Watching Issues

# Check file permissions
ls -la public_html/

# Restart file watcher
npm run pm2:restart

4. Playwright Issues

# Reinstall browsers
npx playwright install --force

# Clear test cache
rm -rf tests/results/

Debug Mode

Enable debug mode for troubleshooting:

# Enable debug logging
export DEBUG=*
npm run dev:pm2

Support

For support and questions:

Waves and Algorithms Development Server Setup Guide