-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (124 loc) · 4.06 KB
/
server.js
File metadata and controls
147 lines (124 loc) · 4.06 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// ============================================
// RESTAURANT BACKEND
// Node.js + Express + MySQL
// Simple Restaurant Management System
// ============================================
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const { testConnection } = require('./config/database');
// Import routes
const menuRoutes = require('./routes/menu');
const reservationRoutes = require('./routes/reservations');
// Initialize Express app
const app = express();
const PORT = process.env.PORT || 3000;
// ============================================
// MIDDLEWARE
// ============================================
// CORS - Allow all origins for development
app.use(cors());
// Body parser middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Request logging middleware
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
next();
});
// ============================================
// HEALTH CHECK ENDPOINT
// ============================================
app.get('/health', (req, res) => {
res.status(200).json({
success: true,
message: 'Restaurant Backend API is running',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});
// ============================================
// API ROUTES
// ============================================
app.use('/api/menu', menuRoutes);
app.use('/api/reservations', reservationRoutes);
// ============================================
// ROOT ENDPOINT
// ============================================
app.get('/', (req, res) => {
res.status(200).json({
success: true,
message: 'Welcome to Restaurant Management API',
version: '1.0.0',
endpoints: {
health: 'GET /health',
menu: {
getAll: 'GET /api/menu',
getById: 'GET /api/menu/:id',
create: 'POST /api/menu'
},
reservations: {
getAll: 'GET /api/reservations',
create: 'POST /api/reservations'
}
}
});
});
// ============================================
// ERROR HANDLING
// ============================================
// 404 Handler
app.use((req, res) => {
res.status(404).json({
success: false,
error: 'Endpoint not found',
path: req.path
});
});
// Global Error Handler
app.use((err, req, res, next) => {
console.error('Error:', err);
res.status(500).json({
success: false,
error: 'Internal server error',
message: err.message
});
});
// ============================================
// START SERVER
// ============================================
const startServer = async () => {
try {
// Test database connection
const dbConnected = await testConnection();
if (!dbConnected) {
console.error('❌ Failed to connect to database. Please check your configuration.');
process.exit(1);
}
// Start Express server
app.listen(PORT, () => {
console.log('============================================');
console.log('🍽️ RESTAURANT BACKEND API');
console.log('============================================');
console.log(`✅ Server running on port ${PORT}`);
console.log(`📍 Local: http://localhost:${PORT}`);
console.log(`🏥 Health check: http://localhost:${PORT}/health`);
console.log(`📚 API docs: http://localhost:${PORT}/`);
console.log('============================================');
});
} catch (error) {
console.error('❌ Failed to start server:', error);
process.exit(1);
}
};
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received. Shutting down gracefully...');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('SIGINT received. Shutting down gracefully...');
process.exit(0);
});
// Start the server
startServer();