const express = require('express'); const cors = require('cors'); // นำเข้า CORS const app = express(); const PORT = 7860; // Port บังคับสำหรับ Hugging Face Spaces // --- การตั้งค่า Middleware --- // อนุญาตให้ทุกคน (ทุก Origin) เข้าถึง API นี้ได้ app.use(cors()); // ทำให้ Express อ่าน JSON ได้ (เวลาเราส่ง Data มาจากหน้าบ้าน) app.use(express.json()); // --- สร้าง Route สำหรับทดสอบ --- // 1. หน้าแรกสำหรับเช็คสถานะ app.get('/', (req, res) => { const start = Date.now(); res.json({ message: "Poop AI Express API is Live!", access: "Public (CORS Enabled)", timestamp: new Date().toISOString(), latency_test: `${Date.now() - start}ms` }); }); // 2. Endpoint สำหรับเทสความเร็วตอนส่งข้อมูล app.post('/test-speed', (req, res) => { res.json({ status: "received", server_time: new Date().toLocaleTimeString(), your_data: req.body }); }); // สั่งให้ Server เริ่มทำงาน app.listen(PORT, '0.0.0.0', () => { console.log(`Server is running on http://0.0.0.0:${PORT}`); console.log(`CORS is enabled for all origins.`); });