-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (66 loc) · 1.9 KB
/
index.js
File metadata and controls
79 lines (66 loc) · 1.9 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
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const cookieParser = require("cookie-parser");
const ConnectDB = require("./src/DataBase/db");
const notesRoute = require("./src/router/notes.router");
const authRouter = require("./src/router/auth.router");
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Connect to Database
ConnectDB();
// Routes
app.get("/", (req, res) => {
res.status(200).json({
message: "CRUD Notes API is running!",
version: "1.0.0",
endpoints: {
auth: "/api/user",
notes: "/api/v1/notes"
}
});
});
// Health check endpoint for Docker
app.get("/health", (req, res) => {
res.status(200).json({
status: "healthy",
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: process.env.NODE_ENV || 'development'
});
});
app.use("/api/v1/notes", notesRoute);
app.use("/api/user", authRouter);
// Global error handler
app.use((err, req, res, next) => {
console.error("Global error:", err.stack);
res.status(500).json({ error: "Something went wrong!" });
});
// Handle 404
app.use("*", (req, res) => {
res.status(404).json({ error: "Route not found" });
});
// Start server only if this file is run directly (not imported)
if (require.main === module) {
const server = app.listen(PORT, () => {
console.log(`Server listening on Port ${PORT}`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received. Shutting down gracefully...');
server.close(() => {
console.log('Process terminated');
});
});
process.on('SIGINT', () => {
console.log('SIGINT received. Shutting down gracefully...');
server.close(() => {
console.log('Process terminated');
});
});
}
module.exports = app;