From c2cc25ec8715494718d4045926576a35f84b0fa5 Mon Sep 17 00:00:00 2001 From: Tassana Pralao <65160024@go.buu.ac.th> Date: Sun, 23 Mar 2025 12:31:57 +0700 Subject: [PATCH] update server.js --- server/server.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/server/server.js b/server/server.js index 3fb13260..48998469 100644 --- a/server/server.js +++ b/server/server.js @@ -4,9 +4,9 @@ const app = express(); const morgan = require("morgan"); const { readdirSync } = require("fs"); const cors = require("cors"); -// const authRouter = require('./routes/auth'); -// const categoryRouter = require('./routes/category'); +const path = require("path"); +// เชื่อมต่อฐานข้อมูล MySQL const db = mysql.createConnection({ host: "node77730-project-65160024.th2.melon.cloud", port: "11818", @@ -15,22 +15,21 @@ const db = mysql.createConnection({ database: "borrow_project", }); -//middleware +// Middleware app.use(morgan("dev")); app.use(express.json({ limit: "20mb" })); - -// การตั้งค่า CORS app.use(cors()); -// app.use('/api', authRouter) -// app.use('/api', categoryRouter) -const path = require("path"); -readdirSync(path.join(__dirname, "routes")).map((c) => - app.use("/api", require(path.join(__dirname, "routes", c))) -); +// โหลด router จากโฟลเดอร์ routes +const routesPath = path.join(__dirname, "routes"); +readdirSync(routesPath).map((file) => { + const routePath = path.join(routesPath, file); + app.use("/api", require(routePath)); +}); -app.listen(3000, () => console.log("server is running on port 3000")); +app.listen(3000, () => console.log("Server is running on port 3000")); +// เชื่อมต่อกับฐานข้อมูล db.connect((err) => { if (err) { console.error("❌ ไม่สามารถเชื่อมต่อฐานข้อมูล:", err); @@ -38,4 +37,3 @@ db.connect((err) => { } console.log("✅ เชื่อมต่อฐานข้อมูลสำเร็จ!"); }); - -- GitLab