forked from Akash-2176/SakthiAuto-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (39 loc) · 1.18 KB
/
server.js
File metadata and controls
48 lines (39 loc) · 1.18 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
const authRoutes = require('./routes/auth');
const itemRoutes = require('./routes/item');
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(express.json());
// MongoDB Connection
mongoose.connect(process.env.MONGODB_URI)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.error('MongoDB Connection Error:', err));
// Routes
app.use('/api/auth', authRoutes);
app.use('/api/items', itemRoutes);
// Health check route
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
mongodb: mongoose.connection.readyState === 1 ? 'connected' : 'disconnected',
timestamp: new Date().toISOString()
});
});
// Error Handler
app.use((err, req, res, next) => {
res.status(500).json({ message: 'Internal server error' });
});
// 404 Handler
app.use('*', (req, res) => {
res.status(404).json({ message: 'Route not found' });
});
// Start
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});