diff --git a/Client/src/apiconfig.js b/Client/src/apiconfig.js index eeed964..07034be 100644 --- a/Client/src/apiconfig.js +++ b/Client/src/apiconfig.js @@ -1 +1 @@ -export const API_BASE_URL = "http://localhost:8000/api"; \ No newline at end of file +export const API_BASE_URL = "https://hotel-management-system-q26a.vercel.app/api"; diff --git a/Client/src/screens/RoomManaagemnt/RoomInventory.jsx b/Client/src/screens/RoomManaagemnt/RoomInventory.jsx index 067e54a..0e26fac 100644 --- a/Client/src/screens/RoomManaagemnt/RoomInventory.jsx +++ b/Client/src/screens/RoomManaagemnt/RoomInventory.jsx @@ -118,6 +118,7 @@ const RoomInventory = () => { } }; + // Fetch rates const fetchRates = async () => { try { const response = await axios.get(RATES_API_BASE_URL); @@ -1055,4 +1056,4 @@ const RoomInventory = () => { ); }; -export default RoomInventory; \ No newline at end of file +export default RoomInventory; diff --git a/Client/src/screens/RoomManaagemnt/RoomRate.jsx b/Client/src/screens/RoomManaagemnt/RoomRate.jsx index 5d4a038..98714e0 100644 --- a/Client/src/screens/RoomManaagemnt/RoomRate.jsx +++ b/Client/src/screens/RoomManaagemnt/RoomRate.jsx @@ -1574,4 +1574,4 @@ const RoomRate = ({ sidebarOpen, setSidebarOpen, sidebarMinimized, setSidebarMin ); }; -export default RoomRate; \ No newline at end of file +export default RoomRate; diff --git a/Client/src/screens/RoomManaagemnt/RoomStatus.jsx b/Client/src/screens/RoomManaagemnt/RoomStatus.jsx index d095226..e8fff0a 100644 --- a/Client/src/screens/RoomManaagemnt/RoomStatus.jsx +++ b/Client/src/screens/RoomManaagemnt/RoomStatus.jsx @@ -786,4 +786,4 @@ const RoomStatus = ({ sidebarOpen, setSidebarOpen, sidebarMinimized, setSidebarM ); }; -export default RoomStatus; \ No newline at end of file +export default RoomStatus; diff --git a/Client/vite.config.js b/Client/vite.config.js index b64ec73..03220aa 100644 --- a/Client/vite.config.js +++ b/Client/vite.config.js @@ -8,7 +8,7 @@ export default defineConfig({ port: 5173, open:true, proxy: { '/api': { - target: 'http://localhost:8000', + target: 'https://hotel-management-system-q26a.vercel.app', changeOrigin: true, }, }, diff --git a/Server/server.js b/Server/api/index.js similarity index 62% rename from Server/server.js rename to Server/api/index.js index 9c0b390..39f4823 100644 --- a/Server/server.js +++ b/Server/api/index.js @@ -1,9 +1,9 @@ const express = require('express'); const mongoose = require('mongoose'); const cors = require('cors'); -const bodyParser = require('body-parser'); require('dotenv').config(); +// --- IMPORT ROUTES --- const roomRoutes = require('./routes/RoomManaagemnt/roomRoutes'); const roomRateRoutes = require('./routes/RoomManaagemnt/roomRateRoutes'); const maintenanceRoutes = require('./routes/RoomManaagemnt/maintenanceRoutes'); @@ -23,7 +23,6 @@ const authRoutes = require('./routes/auth'); const usersRoutes = require('./routes/users'); const app = express(); - // Middleware app.use(cors({ origin: ['http://localhost:5173'], @@ -33,7 +32,7 @@ app.use(cors({ })); app.use(express.json({ limit: '10mb' })); -// Routes +// --- ROUTES --- app.use('/api/rooms', roomRoutes); app.use('/api/room-rates', roomRateRoutes); app.use('/api/roomMaintenance', maintenanceRoutes); @@ -53,25 +52,55 @@ app.use('/api/settings', settingsRoutes); app.use('/api/auth', authRoutes); app.use('/api/users', usersRoutes); -// Error handling middleware +// --- MONGODB CONNECTION --- +const connectDB = async () => { + try { + await mongoose.connect(process.env.MONGO_URI, { dbName: 'hotel-management' }); + console.log('✅ MongoDB connected successfully'); + } catch (err) { + console.error('❌ MongoDB connection error:', err); + } +}; + +connectDB(); + +// Connection event logs +mongoose.connection.on('connected', () => console.log('🟢 MongoDB Connected')); +mongoose.connection.on('disconnected', () => console.log('🔴 MongoDB Disconnected')); +mongoose.connection.on('error', (err) => console.error('⚠️ MongoDB Error:', err)); + +// Keep-alive ping every 5 mins +setInterval(() => { + if (mongoose.connection.readyState !== 1) { + console.log('⏳ Reconnecting MongoDB...'); + connectDB(); + } +}, 5 * 60 * 1000); + +// --- HEALTH CHECK ENDPOINT --- +app.get(['/', '/api/health'], async (req, res) => { + const mongoStates = ['Disconnected','Connected','Connecting','Disconnecting']; + + const waitForConnection = async () => { + let retries = 5; + while(mongoose.connection.readyState !== 1 && retries > 0){ + await new Promise(r => setTimeout(r, 1000)); // wait 1 sec + retries--; + } + }; + await waitForConnection(); + + res.status(200).json({ + status: '✅ API Health: OK', + mongoDB: mongoStates[mongoose.connection.readyState], + environment: process.env.NODE_ENV || 'Development', + timestamp: new Date().toISOString() + }); +}); + app.use((err, req, res, next) => { - console.error(err.stack); + console.error('🔥 Server Error:', err.stack); res.status(500).json({ message: 'Something went wrong!', error: err.message }); }); -// Connect to MongoDB -mongoose - .connect(process.env.MONGO_URI, { - dbName: 'hotel-management', - }) - .then(() => console.log('DB connect successful')) - .catch((err) => { - console.error('DB connection error:', err); - process.exit(1); - }); - -// Start server -const PORT = process.env.PORT || 8000; -app.listen(PORT, () => { - console.log(`Server is running on port ${PORT}`); -}); \ No newline at end of file +module.exports = app; diff --git a/Server/package.json b/Server/package.json index 937efc7..2d1f070 100644 --- a/Server/package.json +++ b/Server/package.json @@ -1,13 +1,14 @@ { "name": "server", "version": "1.0.0", - "main": "index.js", + "main": "api/index.js", "files": [ "rooms.json" ], "scripts": { + "dev": "nodemon api/index.js", "test": "echo \"Error: no test specified\" && exit 1", - "start": "nodemon server.js" + "start": "node api/index.js" }, "keywords": [], "author": "", diff --git a/Server/vercel.json b/Server/vercel.json new file mode 100644 index 0000000..0bb1af1 --- /dev/null +++ b/Server/vercel.json @@ -0,0 +1,9 @@ +{ + "version": 2, + "builds": [ + { "src": "api/index.js", "use": "@vercel/node" } + ], + "routes": [ + { "src": "/(.*)", "dest": "api/index.js" } + ] +}