forked from saurabhd2106/sample-node-app-ci-lab-ih
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (46 loc) · 1.46 KB
/
server.js
File metadata and controls
56 lines (46 loc) · 1.46 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
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Disable X-Powered-By header for security
app.disable('x-powered-by');
// Serve static files from the public directory
app.use(express.static(path.join(__dirname, 'public')));
// Set up middleware for logging
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
next();
});
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});
// Serve the main page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Handle 404 errors
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, 'public', '404.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
error: 'Something went wrong!',
message: process.env.NODE_ENV === 'development' ? err.message : 'Internal server error'
});
});
// Only start the server if this file is run directly
if (require.main === module) {
app.listen(PORT, () => {
console.log(`🚀 DevOps Bootcamp Showcase server running on http://localhost:${PORT}`);
console.log(`📊 Health check available at http://localhost:${PORT}/health`);
});
}
// Export the app for testing
module.exports = app;