|
| 1 | +# Serving Static Web Apps with Launchpad Monitor |
| 2 | + |
| 3 | +This recipe demonstrates how to use Launchpad's monitor functionality to serve static web applications using PM2's built-in serve capability, and optionally launch a browser to display them in kiosk mode. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +```typescript |
| 8 | +import { defineConfig } from '@bluecadet/launchpad-cli'; |
| 9 | +import path from 'path'; |
| 10 | +import { homedir } from 'os'; |
| 11 | + |
| 12 | +export default defineConfig({ |
| 13 | + monitor: { |
| 14 | + apps: [ |
| 15 | + // Static web server |
| 16 | + { |
| 17 | + pm2: { |
| 18 | + name: "webapp-server", |
| 19 | + script: "serve", // Uses PM2's built-in serve functionality |
| 20 | + cwd: "./apps/webapp", // Path to your web app folder |
| 21 | + env: { |
| 22 | + PM2_SERVE_PATH: "./dist/", // Path to static files |
| 23 | + PM2_SERVE_PORT: "8080", // Port to serve on |
| 24 | + PM2_SERVE_SPA: "true", // Enable single-page app mode |
| 25 | + PM2_SERVE_HOMEPAGE: "/index.html" // Default page |
| 26 | + } |
| 27 | + } |
| 28 | + }, |
| 29 | + |
| 30 | + // Browser to display the web app (optional) |
| 31 | + { |
| 32 | + pm2: { |
| 33 | + name: "webapp-browser", |
| 34 | + // Path to Chromium (adjust for your environment) |
| 35 | + cwd: path.join(homedir(), "AppData/Local/Chromium/Application"), |
| 36 | + script: "chrome.exe", |
| 37 | + args: "--kiosk --incognito --disable-pinch --overscroll-history-navgation=0 --enable-auto-reload --autoplay-policy=no-user-gesture-required http://localhost:8080" |
| 38 | + }, |
| 39 | + windows: { |
| 40 | + foreground: true // Bring browser to foreground |
| 41 | + }, |
| 42 | + logging: { |
| 43 | + showStdout: false, // Reduce console noise |
| 44 | + showStderr: false |
| 45 | + } |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +## How It Works |
| 53 | + |
| 54 | +### The Server App |
| 55 | + |
| 56 | +The first app in the configuration uses PM2's built-in `serve` functionality: |
| 57 | + |
| 58 | +- `script: "serve"` tells PM2 to use its static file server |
| 59 | +- `PM2_SERVE_PATH` specifies which directory to serve |
| 60 | +- `PM2_SERVE_PORT` sets the port number |
| 61 | +- `PM2_SERVE_SPA: "true"` enables Single Page App support (routes all requests to index.html) |
| 62 | +- `PM2_SERVE_HOMEPAGE` sets the default page |
| 63 | + |
| 64 | +### The Browser App (optional) |
| 65 | + |
| 66 | +The second app launches a browser to display your web content: |
| 67 | + |
| 68 | +- Uses Chromium in kiosk mode (full-screen, minimal UI) |
| 69 | +- Includes flags to optimize for exhibition use |
| 70 | +- Automatically navigates to the locally served content |
| 71 | +- Is configured to stay in the foreground |
| 72 | + |
| 73 | +## Common Customizations |
| 74 | + |
| 75 | +### Change the Port |
| 76 | + |
| 77 | +```javascript |
| 78 | +env: { |
| 79 | + PM2_SERVE_PORT: "3000", // Change to your preferred port |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +Don't forget to update the URL in the browser args too! |
| 84 | + |
| 85 | +### Enable HTTPS |
| 86 | + |
| 87 | +```javascript |
| 88 | +env: { |
| 89 | + PM2_SERVE_PATH: "./dist/", |
| 90 | + PM2_SERVE_PORT: "8443", |
| 91 | + PM2_SERVE_SPA: "true", |
| 92 | + PM2_SERVE_HOMEPAGE: "/index.html", |
| 93 | + PM2_SERVE_SSL_KEY: "./path/to/key.pem", // Path to SSL key |
| 94 | + PM2_SERVE_SSL_CERT: "./path/to/cert.pem" // Path to SSL certificate |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Chrome/Chromium Flags |
| 99 | + |
| 100 | +Common flags for kiosk applications: |
| 101 | + |
| 102 | +- `--kiosk`: Full-screen kiosk mode |
| 103 | +- `--incognito`: Private browsing mode |
| 104 | +- `--disable-pinch`: Disable pinch zoom |
| 105 | +- `--overscroll-history-navigation=0`: Disable swipe navigation |
| 106 | +- `--enable-auto-reload`: Auto-refresh on crashes |
| 107 | +- `--autoplay-policy=no-user-gesture-required`: Allow autoplay of media |
| 108 | + |
| 109 | +### Using Other Browsers |
| 110 | + |
| 111 | +For Firefox: |
| 112 | + |
| 113 | +```javascript |
| 114 | +{ |
| 115 | + pm2: { |
| 116 | + name: "webapp-firefox", |
| 117 | + cwd: "C:/Program Files/Mozilla Firefox", |
| 118 | + script: "firefox.exe", |
| 119 | + args: "-kiosk http://localhost:8080" |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Troubleshooting |
| 125 | + |
| 126 | +### Server won't start |
| 127 | + |
| 128 | +- Ensure the `serve` module is available to PM2 |
| 129 | +- Check that the specified path contains static web files |
| 130 | +- Verify the port isn't already in use |
| 131 | + |
| 132 | +### Browser doesn't launch |
| 133 | + |
| 134 | +- Verify the browser path is correct for your system |
| 135 | +- Check that the URL is correct and the server is running |
| 136 | +- Some browser flags may differ between versions |
| 137 | + |
| 138 | +### Page doesn't load properly |
| 139 | + |
| 140 | +- If using SPA mode, ensure your app is built as a proper SPA |
| 141 | +- For non-SPA sites, set `PM2_SERVE_SPA` to `"false"` |
| 142 | + |
| 143 | + |
0 commit comments