-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from Nintails-TF/API/further-API-improvements
API/further-API-improvements
- Loading branch information
Showing
5 changed files
with
66 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}!`)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.