-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (114 loc) · 3.27 KB
/
Copy pathserver.js
File metadata and controls
131 lines (114 loc) · 3.27 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// -- Otel Instrumentation provides spans for worker management--
const nsolid = require('nsolid')
const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb')
const api = require('@opentelemetry/api')
// Register the OpenTelemetry api with nsolid
if (!nsolid.otel.register(api)) {
throw new Error('Failed to register NSOLID OpenTelemetry')
}
// Register the desired instrumentation with the N|Solid native implementation
nsolid.otel.registerInstrumentations([
new MongoDBInstrumentation()
])
const express = require('express')
const cors = require('cors')
const path = require('path')
const database = require('./database')
const productsRouter = require('./routes/products')
const app = express()
const PORT = process.env.PORT || 3000
// Middleware
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// Serve static files
app.use(express.static(path.join(__dirname, 'public')))
// Routes
app.use('/api/products', productsRouter)
// Health check endpoint
app.get('/api/health', async (req, res) => {
try {
const isDbConnected = await database.testConnection()
res.json({
success: true,
message: 'Server is running',
database: isDbConnected ? 'connected' : 'disconnected',
timestamp: new Date().toISOString()
})
} catch (error) {
res.status(500).json({
success: false,
message: 'Health check failed',
error: error.message
})
}
})
// API documentation endpoint
app.get('/api', (req, res) => {
res.json({
message: 'MongoDB Demo API',
version: '1.0.0',
endpoints: {
'GET /api/health': 'Health check',
'GET /api/products': 'Get all products',
'GET /api/products/:id': 'Get product by ID',
'POST /api/products': 'Create new product',
'PUT /api/products/:id': 'Update product',
'DELETE /api/products/:id': 'Delete product'
},
sampleProduct: {
name: 'Sample Product',
price: 29.99,
category: 'Electronics',
inStock: true
}
})
})
// Root endpoint
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Error:', err)
res.status(500).json({
success: false,
message: 'Internal server error',
error: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
})
})
// 404 handler
app.use((req, res) => {
res.status(404).json({
success: false,
message: 'Endpoint not found'
})
})
// Start server
async function startServer () {
try {
// Connect to database
await database.connect()
// Start Express server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`)
console.log(`API documentation: http://localhost:${PORT}/api`)
console.log(`Health check: http://localhost:${PORT}/api/health`)
})
} catch (error) {
console.error('Failed to start server:', error)
process.exit(1)
}
}
// Graceful shutdown
process.on('SIGINT', async () => {
console.log('\nShutting down gracefully...')
await database.disconnect()
process.exit(0)
})
process.on('SIGTERM', async () => {
console.log('\nShutting down gracefully...')
await database.disconnect()
process.exit(0)
})
startServer()