STRUKTUR PROJECT (DISARANKAN)
api-wa/
 ├── db.js
 ├── server.js
 └── routes/
      └── messages.js


FILE KONEKSI DATABASE

📄 db.js

import mysql from "mysql2/promise";

const db = mysql.createPool({
  host: "localhost",
  user: "nokit",
  password: "tyughjbnm123",
  database: "api-wa",
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
});

export default db;


ENDPOINT MESSAGES (GET)

📄 routes/messages.js

import express from "express";
import db from "../db.js";

const router = express.Router();

// GET /api/messages
router.get("/", async (req, res) => {
  try {
    const [rows] = await db.query(
      `SELECT 
        id,
        sender,
        message,
        created_at
       FROM messages
       ORDER BY created_at DESC`
    );

    res.json({
      status: true,
      data: rows
    });
  } catch (error) {
    console.error(error);
    res.status(500).json({
      status: false,
      message: "Gagal mengambil data messages"
    });
  }
});

export default router;



FILE SERVER UTAMA

📄 server.js

import express from "express";
import cors from "cors";

import messagesRoute from "./routes/messages.js";

const app = express();

app.use(cors());
app.use(express.json());

app.use("/api/messages", messagesRoute);

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`🚀 API running on http://localhost:${PORT}`);
});

🟢 5️⃣ CONTOH STRUKTUR TABEL messages

Pastikan tabel Anda mirip ini:

CREATE TABLE messages (
  id INT AUTO_INCREMENT PRIMARY KEY,
  sender VARCHAR(100),
  message TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

🟢 6️⃣ TEST ENDPOINT

Jalankan server:

node server.js


Tes di browser / Postman:

http://localhost:3000/api/messages


Output:

{
  "status": true,
  "data": [
    {
      "id": 1,
      "sender": "Admin",
      "message": "Halo dari database",
      "created_at": "2026-01-13T02:00:00.000Z"
    }
  ]
}

🟦 7️⃣ KONEKSI KE FLUTTER (INGAT INI PENTING)
📌 URL API di Flutter

Android Emulator

http://10.0.2.2:3000/api/messages


HP Asli

http://IP_KOMPUTER_ANDA:3000/api/messages

🔥 8️⃣ NEXT STEP (PALING LOGIS)

Setelah GET berhasil, biasanya langsung lanjut:

➕ POST message

✏️ UPDATE message

❌ DELETE message

🔐 JWT Auth

🔴 Realtime (Socket.IO / Baileys)

👉 Bilang saja:
“Lanjut POST messages + Flutter form”
Saya akan buatkan endpoint + UI Flutter siap pakai 💪




ChatGPT can make mistakes. Chec