46 lines
2.1 KiB
JavaScript
46 lines
2.1 KiB
JavaScript
// 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 sending to Telegram.
|
|
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 budget = String(body.budget ?? '').trim();
|
|
if (!name || !contact || !message) {
|
|
return send(res, 422, false, 'name, contact, message — обязательные поля.');
|
|
}
|
|
console.log('\n=== НОВОЕ СООБЩЕНИЕ (stub) ===');
|
|
console.log('Имя: ', name);
|
|
console.log('Связь: ', contact);
|
|
console.log('Бюджет: ', budget || '—');
|
|
console.log('Задача: ', message);
|
|
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, budget?}');
|
|
});
|