-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (59 loc) · 1.62 KB
/
index.js
File metadata and controls
70 lines (59 loc) · 1.62 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
import express from 'express';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware for parsing JSON requests
app.use(express.json());
// Basic route
app.get('/', (req, res) => {
res.json({
message: 'App is working! 🚀',
status: 'success',
timestamp: new Date().toISOString(),
environment: process.env.NODE_ENV || 'development',
port: PORT
});
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
uptime: process.uptime(),
timestamp: new Date().toISOString()
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({
message: 'Route not found',
status: 'error',
path: req.originalUrl
});
});
// Global error handler
app.use((err, req, res, next) => {
console.error('Error:', err.message);
console.error('Stack:', err.stack);
res.status(err.status || 500).json({
message: 'Internal server error',
status: 'error',
...(process.env.NODE_ENV === 'development' && { error: err.message })
});
});
// Start the server
app.listen(PORT, () => {
console.log(`🚀 Server is running on port ${PORT}`);
console.log(`📍 Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`🌐 Access the app at: http://localhost:${PORT}`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
console.log('👋 SIGTERM received, shutting down gracefully');
process.exit(0);
});
process.on('SIGINT', () => {
console.log('👋 SIGINT received, shutting down gracefully');
process.exit(0);
});