-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (44 loc) · 1.13 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
import express from 'express';
import userRoutes from './routes/user.route.js';
import authRoutes from './routes/auth.route.js';
import cookieParser from 'cookie-parser';
import connectMongoDB from './config/mongoose.js';
import helmet from "helmet";
import cors from "cors";
// Connect to MongoDB
connectMongoDB();
// Create an Express app
const app = express();
// Middleware
app.use(express.json());
app.use(cookieParser());
app.use(helmet());
app.use(cors({
origin: 'http://localhost:3000',
}));
// Start the server
app.listen(3000 || process.env.PORT, () => {
console.log('Server listening on port 3000');
});
// Test route
app.get('/test', (req, res) => {
return res.json({
success: true,
message: 'Test successful!',
});
});
// Routes
app.use('/api/user', userRoutes);
app.use('/api/auth', authRoutes);
// Error handling middleware
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const message = err.message || 'Internal Server Error';
const validation = err.validation || [];
return res.status(statusCode).json({
success: false,
message,
statusCode,
validation,
});
});