|
| 1 | +# Line-of-Sight Tool - Node.js Express API |
| 2 | + |
| 3 | +A Node.js backend for collaborative node editing using Express and JSON file storage. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +### 1. Install Dependencies |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install |
| 11 | +``` |
| 12 | + |
| 13 | +### 2. Run the Server |
| 14 | + |
| 15 | +**Development (with auto-reload on Node 18+):** |
| 16 | +```bash |
| 17 | +npm run dev |
| 18 | +``` |
| 19 | + |
| 20 | +**Production:** |
| 21 | +```bash |
| 22 | +npm start |
| 23 | +``` |
| 24 | + |
| 25 | +The server runs on `http://localhost:3000` |
| 26 | + |
| 27 | +## API Endpoints |
| 28 | + |
| 29 | +| Method | URL | Description | |
| 30 | +|--------|-----|-------------| |
| 31 | +| `POST` | `?action=newkey` | Create a new workspace | |
| 32 | +| `GET` | `?key=XXXXXXXXXXXXXXXX` | Get data for a key | |
| 33 | +| `POST` | `?key=XXXXXXXXXXXXXXXX` | Save data (JSON body) | |
| 34 | +| `GET` | `?action=status` | Get server status | |
| 35 | + |
| 36 | +## Configuration |
| 37 | + |
| 38 | +Edit the `CONFIG` object at the top of `api.js`: |
| 39 | + |
| 40 | +```javascript |
| 41 | +const CONFIG = { |
| 42 | + DATA_DIR: path.join(__dirname, 'data'), |
| 43 | + MAX_KEYS: 50, |
| 44 | + MAX_NODES: 400, |
| 45 | + MAX_GROUPS: 100, |
| 46 | + MAX_JSON_SIZE: 512 * 1024, // 512KB |
| 47 | + KEY_LENGTH: 16, |
| 48 | + PORT: process.env.PORT || 3000 |
| 49 | +}; |
| 50 | +``` |
| 51 | + |
| 52 | +## Deployment Options |
| 53 | + |
| 54 | +### Vercel (Serverless) |
| 55 | + |
| 56 | +Create `vercel.json`: |
| 57 | +```json |
| 58 | +{ |
| 59 | + "version": 2, |
| 60 | + "builds": [{ "src": "api.js", "use": "@vercel/node" }], |
| 61 | + "routes": [{ "src": "/(.*)", "dest": "api.js" }] |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Note: Vercel is serverless, so you'd need to use a database instead of file storage. |
| 66 | + |
| 67 | +### Railway / Render / Fly.io |
| 68 | + |
| 69 | +These platforms support persistent storage: |
| 70 | + |
| 71 | +```bash |
| 72 | +# Railway |
| 73 | +railway init |
| 74 | +railway up |
| 75 | + |
| 76 | +# Render - create a Web Service pointing to your repo |
| 77 | + |
| 78 | +# Fly.io |
| 79 | +fly launch |
| 80 | +fly deploy |
| 81 | +``` |
| 82 | + |
| 83 | +### PM2 (Linux VPS) |
| 84 | + |
| 85 | +```bash |
| 86 | +# Install PM2 |
| 87 | +npm install -g pm2 |
| 88 | + |
| 89 | +# Start with PM2 |
| 90 | +pm2 start api.js --name los-api |
| 91 | + |
| 92 | +# Auto-start on reboot |
| 93 | +pm2 startup |
| 94 | +pm2 save |
| 95 | + |
| 96 | +# View logs |
| 97 | +pm2 logs los-api |
| 98 | +``` |
| 99 | + |
| 100 | +### Docker |
| 101 | + |
| 102 | +Create `Dockerfile`: |
| 103 | +```dockerfile |
| 104 | +FROM node:20-alpine |
| 105 | +WORKDIR /app |
| 106 | +COPY package*.json ./ |
| 107 | +RUN npm ci --only=production |
| 108 | +COPY api.js ./ |
| 109 | +RUN mkdir -p data |
| 110 | +EXPOSE 3000 |
| 111 | +CMD ["node", "api.js"] |
| 112 | +``` |
| 113 | + |
| 114 | +Build and run: |
| 115 | +```bash |
| 116 | +docker build -t los-api . |
| 117 | +docker run -p 3000:3000 -v $(pwd)/data:/app/data los-api |
| 118 | +``` |
| 119 | + |
| 120 | +### Systemd Service (Linux VPS) |
| 121 | + |
| 122 | +Create `/etc/systemd/system/los-api.service`: |
| 123 | +```ini |
| 124 | +[Unit] |
| 125 | +Description=Line-of-Sight Collaboration API |
| 126 | +After=network.target |
| 127 | + |
| 128 | +[Service] |
| 129 | +Type=simple |
| 130 | +User=www-data |
| 131 | +WorkingDirectory=/path/to/node-api |
| 132 | +ExecStart=/usr/bin/node api.js |
| 133 | +Restart=on-failure |
| 134 | +Environment=NODE_ENV=production |
| 135 | +Environment=PORT=3000 |
| 136 | + |
| 137 | +[Install] |
| 138 | +WantedBy=multi-user.target |
| 139 | +``` |
| 140 | + |
| 141 | +Then: |
| 142 | +```bash |
| 143 | +sudo systemctl enable los-api |
| 144 | +sudo systemctl start los-api |
| 145 | +``` |
| 146 | + |
| 147 | +Use nginx as reverse proxy for HTTPS. |
| 148 | + |
| 149 | +### Nginx Reverse Proxy |
| 150 | + |
| 151 | +```nginx |
| 152 | +server { |
| 153 | + listen 443 ssl; |
| 154 | + server_name api.yourdomain.com; |
| 155 | +
|
| 156 | + ssl_certificate /path/to/cert.pem; |
| 157 | + ssl_certificate_key /path/to/key.pem; |
| 158 | +
|
| 159 | + location / { |
| 160 | + proxy_pass http://127.0.0.1:3000; |
| 161 | + proxy_http_version 1.1; |
| 162 | + proxy_set_header Upgrade $http_upgrade; |
| 163 | + proxy_set_header Connection 'upgrade'; |
| 164 | + proxy_set_header Host $host; |
| 165 | + proxy_set_header X-Real-IP $remote_addr; |
| 166 | + proxy_cache_bypass $http_upgrade; |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## Data Storage |
| 172 | + |
| 173 | +All data is stored as JSON files in the `data/` directory: |
| 174 | + |
| 175 | +``` |
| 176 | +data/ |
| 177 | + ABC123xyz789pqrs.json |
| 178 | + XYZ789abc123mnop.json |
| 179 | +``` |
| 180 | + |
| 181 | +## Environment Variables |
| 182 | + |
| 183 | +| Variable | Default | Description | |
| 184 | +|----------|---------|-------------| |
| 185 | +| `PORT` | 3000 | Server port | |
| 186 | + |
| 187 | +## Backup |
| 188 | + |
| 189 | +```bash |
| 190 | +cp -r data/ backup-$(date +%Y%m%d)/ |
| 191 | +``` |
| 192 | + |
| 193 | +## Security Notes |
| 194 | + |
| 195 | +- Keys are 16 alphanumeric characters |
| 196 | +- All input is validated and sanitized |
| 197 | +- Path traversal is prevented |
| 198 | +- CORS is open by default - restrict in production if needed |
| 199 | +- Use HTTPS in production (via nginx/cloudflare) |
0 commit comments