-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (63 loc) · 2.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require("dotenv").config()
const express = require("express");
const dotenv = require("dotenv");
const cookieParser = require('cookie-parser');
const { verifyToken, verifyUser, verifyAdmin } = require('./utils/verifyToken');
const db = process.env.MONGO;
const path = require("path")
// Import routes
const authRoute = require('./routes/auth');
const usersRoute = require('./routes/users');
const hotelsRoute = require('./routes/hotels');
const roomsRoute = require('./routes/rooms');
const app = express();
// Database connection with mongodb
//connecting mongodb with express server
const mongoose = require("mongoose");
mongoose.connect(db)
.then(() => console.log('MongoDB connected...'))
.catch(err => {
console.error('MongoDB connection error:', err);
console.error('Error code:', err.code);
console.error('Error message:', err.message);
});
//telling backend to look for frontend here in this folder
app.use(express.static(path.resolve(__dirname, 'frontend-booking', 'build')))
app.get("/test",(req,res)=>{
res.send("Express app is running")
})
// Middleware
app.use(cookieParser());
app.use(express.json());
// Route Middleware
app.use("/api/auth", authRoute);
app.use("/api/users", usersRoute);
app.use("/api/hotels", hotelsRoute);
app.use("/api/rooms", roomsRoute);
// Error handling middleware
app.use((err, req, res, next) => {
const errorStatus = err.status || 500;
const errorMessage = err.message || "Something went wrong";
return res.status(errorStatus).json({
success: false,
status: errorStatus,
message: errorMessage,
stack: err.stack,
});
});
//serving frontend routes first
app.get('*', (req, res) => {
res.sendFile(
path.resolve(__dirname, 'frontend', 'build', 'index.html'),
function (err) {
if (err) {
res.status(500).send(err)
}
}
)
});
// Start the server
app.listen(8000, () => {
//connect();
console.log('Server is running on port 8000');
});