-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (44 loc) · 1.36 KB
/
app.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
import express from "express";
import cookieParser from "cookie-parser";
import cors from "cors";
import helmet from "helmet";
import auth_route from "./routes/auth.js"; // Importing authentication routes
import protect from "./middleware/protectRoute.js";
import dotenv from "dotenv";
dotenv.config();
const app = express();
app.use(express.json()); // Parses incoming JSON requests
// CORS Configuration
const corsOptions = {
origin: process.env.FRONTEND_URL,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
allowedHeaders: ['Content-Type', 'Authorization'],
};
app.use(cors(corsOptions));
app.use(cookieParser());
app.get('', (req, res) => {
res.json({ message: 'Backend is running!' });
});
// Logging Middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
// Route Definitions
app.use("/api/v1/auth", auth_route); // Authentication routes
// Error Handling Middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(err.status || 500).json({
status: "error",
message: err.message || "Internal Server Error",
});
});
process.on('SIGTERM', () => {
console.log('SIGTERM signal received: closing HTTP server');
server.close(() => {
console.log('HTTP server closed');
});
});
export default app; // Exports the app for use in server.js