-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
43 lines (34 loc) · 1.18 KB
/
Copy pathapp.js
File metadata and controls
43 lines (34 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
import express from 'express';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import 'dotenv/config';
import connectDB from './model/mongodb.js';
import { globalLimiter } from './middleware/rateLimiter.js';
import { env } from './config/env.js';
// Import router for authentication endpoints (login, register, logout, refresh)
import authRoutes from './routes/authRoutes.js'
import errorHandler from './middleware/errorHandler.js';
const app = express();
const port = env.PORT;
// Middleware setup for JSON parsing, CORS with frontend, and cookie handling
app.use(express.json());
app.use(cors({
origin: 'http://localhost:5173', // or your frontend URL whatever it is
credentials: true
}));
app.use(cookieParser());
// Register route handlers with rate limiting and API endpoints
app.use(globalLimiter);
app.use('/api/auth', authRoutes);
// Global error handling middleware
app.use(errorHandler);
// Establish connection to MongoDB database
try {
connectDB();
} catch (error) {
console.error('Failed to connect to MongoDB:', error);
}
// Start the Express server on specified port
app.listen(port, () => {
console.log(`Running on PORT:${port}`);
});