diff --git a/api/api_server.js b/api/api_server.js index 9f02632..2ebc0e7 100644 --- a/api/api_server.js +++ b/api/api_server.js @@ -1,37 +1,33 @@ -require('dotenv').config(); - +require('dotenv').config(); const express = require('express'); -const mongoose = require('mongoose'); -const atmRoutes = require('./atmRoutes'); // Import ATM routes -const branchRoutes = require('./branchRoutes'); // Import Branch routes +const connectDB = require('./database'); // Import the database connection function +const atmRoutes = require('./atmRoutes'); +const branchRoutes = require('./branchRoutes'); const app = express(); -// MongoDB URI -const uri = process.env.MONGO_URI; - -// Middleware app.use(express.json()); -mongoose.connect(uri, { - useNewUrlParser: true, - useUnifiedTopology: true, -}); - -const db = mongoose.connection; -db.on("error", console.error.bind(console, "Connection error:")); -db.once("open", () => { - console.log("Connected to the database"); -}); +connectDB(); // Connect to the database -// Use the imported routes app.use(atmRoutes); app.use(branchRoutes); -// Root endpoint +// Root Endpoint app.get("/", (req, res) => { - res.send("Hello World!"); + res.send("Hello World!"); +}); + +// 404 Error Handler (for any unhandled routes) +app.use((req, res, next) => { + res.status(404).send("Resource not found"); +}); + +// Generic Error Handler +app.use((error, req, res, next) => { + console.error(error.stack); + res.status(500).send('Something broke!'); }); -// Use the PORT environment variable const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(`API server listening on port ${PORT}!`)); + diff --git a/api/atmRoutes.js b/api/atmRoutes.js index 0bc2719..69b63a7 100644 --- a/api/atmRoutes.js +++ b/api/atmRoutes.js @@ -3,6 +3,13 @@ const router = express.Router(); const mongoose = require('mongoose'); const db = mongoose.connection; +// Error handling function +function handleError(res, error) { + console.error(error); + res.status(500).json({ error: error.message }); +} + + // ATM ENDPOINTS // GET all ATMs @@ -12,7 +19,7 @@ router.get("/api/atms", async (req, res) => { console.log(atms); res.json(atms); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -25,7 +32,7 @@ router.get("/api/atms/:id", async (req, res) => { console.log(atm); res.json(atm); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -42,7 +49,7 @@ router.put("/api/atms/:id", async (req, res) => { console.log(updatedAtm); res.json(updatedAtm); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -58,7 +65,7 @@ router.delete("/api/atms/:id", async (req, res) => { console.log(deletedAtm); res.status(204).json(); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -69,7 +76,7 @@ router.post("/api/atms", async (req, res) => { console.log(newAtm); res.status(201).json(newAtm); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); diff --git a/api/branchRoutes.js b/api/branchRoutes.js index 944b85a..44db3c3 100644 --- a/api/branchRoutes.js +++ b/api/branchRoutes.js @@ -3,6 +3,13 @@ const router = express.Router(); const mongoose = require('mongoose'); const db = mongoose.connection; +// Error handling function +function handleError(res, error) { + console.error(error); + res.status(500).json({ error: error.message }); +} + + // BRANCH ENDPOINTS // GET all Branches @@ -12,7 +19,7 @@ router.get("/api/branches", async (req, res) => { console.log(branches); res.json(branches); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -25,7 +32,7 @@ router.get("/api/branches/:id", async (req, res) => { console.log(branch); res.json(branch); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -42,7 +49,7 @@ router.put("/api/branches/:id", async (req, res) => { console.log(updatedBranch); res.json(updatedBranch); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -58,7 +65,7 @@ router.delete("/api/branches/:id", async (req, res) => { console.log(deletedBranch); res.status(204).json(); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); @@ -69,7 +76,7 @@ router.post("/api/branches", async (req, res) => { console.log(newBranch); res.status(201).json(newBranch); } catch (error) { - res.status(500).json({ error: error.message }); + handleError(res, error); } }); diff --git a/api/database.js b/api/database.js new file mode 100644 index 0000000..d6a3351 --- /dev/null +++ b/api/database.js @@ -0,0 +1,17 @@ +const mongoose = require('mongoose'); + +const connectDB = async () => { + try { + const uri = process.env.MONGO_URI; + await mongoose.connect(uri, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); + console.log("Connected to the database"); + } catch (error) { + console.error("Connection error:", error.message); + process.exit(1); + } +}; + +module.exports = connectDB; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..1047ca7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "ncr-agile-wombo", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}