// Local dev stub for the contact form endpoint. // Usage: node api/dev-stub.mjs (listens on http://127.0.0.1:8787) // Point the form's API_URL to http://127.0.0.1:8787/ during development: //
// Same JSON contract as api/contact.php, but logs instead of delivering. import { createServer } from 'node:http'; const PORT = Number(process.env.PORT) || 8787; const HOST = '127.0.0.1'; function send(res, status, ok, msg) { res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' }); res.end(JSON.stringify(ok ? { ok: true, message: msg } : { ok: false, error: msg })); } createServer((req, res) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); if (req.method === 'OPTIONS') { res.writeHead(204); return res.end(); } if (req.method !== 'POST') { return send(res, 405, false, 'Method not allowed.'); } let raw = ''; req.on('data', (c) => { raw += c; if (raw.length > 64 * 1024) req.destroy(); }); req.on('end', () => { let body; try { body = JSON.parse(raw || '{}'); } catch { return send(res, 400, false, 'Expected a JSON body.'); } const name = String(body.name ?? '').trim(); const contact = String(body.contact ?? '').trim(); const message = String(body.message ?? '').trim(); const consent = body.consent === true; const meta = body.meta && typeof body.meta === 'object' ? body.meta : {}; if (!consent) return send(res, 422, false, 'Отметьте согласие на обработку персональных данных.'); if (name.length < 2 || name.length > 100) return send(res, 422, false, 'Укажите имя (2–100 символов).'); if (contact.length < 2 || contact.length > 200) return send(res, 422, false, 'Укажите предпочтительное средство связи (2–200 символов).'); if (message.length < 10 || message.length > 4000) return send(res, 422, false, 'Опишите задачу (10–4000 символов).'); console.log('\n=== НОВОЕ СООБЩЕНИЕ (stub) ==='); console.log('Имя: ', name); console.log('Связь: ', contact); console.log('Задача: ', message); if (Object.keys(meta).length) console.log('Мета: ', JSON.stringify(meta)); console.log('================================\n'); send(res, 200, true, 'Сообщение отправлено (stub). Мы ответим в ближайшее время.'); }); }).listen(PORT, HOST, () => { console.log(`Contact stub: http://${HOST}:${PORT}`); console.log('POST JSON {name, contact, message, consent, website, meta}'); });