# Backend for DROPNAME This directory contains the backend API and WebSocket handlers for your drop. ## Files - **api.js** - REST API endpoints - **schema.sql** - Database schema (SQLite) - **websocket.js** - WebSocket handlers (optional) ## Usage ### REST API The backend provides REST endpoints accessible at: ``` http://localhost:3001/api/DROPNAME/* ``` Example fetch from your drop's frontend: ```javascript // GET request const response = await fetch('/api/DROPNAME/items') const data = await response.json() // POST request const response = await fetch('/api/DROPNAME/items', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ data: { /* your data */ } }) }) ``` ### WebSocket Connect to WebSocket at: ``` ws://localhost:3001/ws/DROPNAME ``` Example WebSocket connection: ```javascript const ws = new WebSocket('ws://localhost:3001/ws/DROPNAME') ws.onopen = () => { console.log('Connected') ws.send(JSON.stringify({ type: 'hello' })) } ws.onmessage = (event) => { const data = JSON.parse(event.data) console.log('Received:', data) } ``` ### Database Tables use the naming convention: `DROPNAME_tablename` Access the database in your API: ```javascript import { dbHelpers } from '../../backend/src/db.js' const { query, queryOne, run } = dbHelpers // Query all rows const items = query('SELECT * FROM DROPNAME_items') // Query single row const item = queryOne('SELECT * FROM DROPNAME_items WHERE id = ?', [id]) // Insert/Update/Delete const result = run('INSERT INTO DROPNAME_items (data) VALUES (?)', [data]) ``` ## Development When you run `npm run dev:drops DROPNAME`, the backend server automatically: 1. Loads your API routes 2. Applies your database schema 3. Sets up WebSocket handlers 4. Hot-reloads when you change backend files ## Environment Variables Create a `.env` file in the root directory to add environment variables: ``` PRODUCTION_URL=https://yourdomain.com MY_API_KEY=your-api-key-here ``` Access in your code: ```javascript const apiKey = process.env.MY_API_KEY ``` ## Tips - Keep your API routes simple and focused - Use proper HTTP status codes (200, 201, 400, 404, 500) - Validate input data before saving to database - Use JSON for request/response bodies - Log errors for debugging - Use WebSockets for real-time features (multiplayer, live updates, etc.)