-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (33 loc) · 974 Bytes
/
Copy pathserver.js
File metadata and controls
42 lines (33 loc) · 974 Bytes
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
'use strict';
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || '0.0.0.0';
app.use(express.json());
// Health endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'ok',
message: 'Service is healthy',
uptime: process.uptime(),
timestamp: new Date().toISOString(),
version: process.env.npm_package_version || '1.0.0',
});
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Not Found', path: req.path });
});
// 500 error handler
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal Server Error' });
});
// Start server only if run directly
if (require.main === module) {
app.listen(PORT, HOST, () => {
console.log(`Server running at http://${HOST}:${PORT}`);
console.log(`Health endpoint: http://${HOST}:${PORT}/health`);
});
}
module.exports = app;