first commit

This commit is contained in:
Kurt Ferreira
2026-04-11 13:57:02 +02:00
commit db5ca09c1b
5 changed files with 189 additions and 0 deletions

65
src/index.js Normal file
View File

@@ -0,0 +1,65 @@
'use strict'
const express = require('express')
const promClient = require('prom-client')
const app = express()
const PORT = process.env.PORT || 3000
// Prometheus metrics
const collectDefaultMetrics = promClient.collectDefaultMetrics
collectDefaultMetrics({ prefix: 'nodejs_app_' })
const httpRequestDuration = new promClient.Histogram({
name: 'http_request_duration_seconds',
help: 'Duration of HTTP requests in seconds',
labelNames: ['method', 'route', 'status'],
buckets: [0.01, 0.05, 0.1, 0.5, 1, 5],
})
const httpRequestTotal = new promClient.Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'route', 'status'],
})
// Metrics middleware
app.use((req, res, next) => {
const end = httpRequestDuration.startTimer()
res.on('finish', () => {
const route = req.route ? req.route.path : req.path
const labels = { method: req.method, route, status: res.statusCode }
end(labels)
httpRequestTotal.inc(labels)
})
next()
})
app.use(express.json())
app.get('/metrics', async (_req, res) => {
res.set('Content-Type', promClient.register.contentType)
res.end(await promClient.register.metrics())
})
app.get('/health', (_req, res) => {
res.json({ status: 'ok' })
})
app.get('/', (_req, res) => {
res.json({
message: 'Hello from Node.js!',
env: process.env.NODE_ENV || 'development',
version: process.env.APP_VERSION || 'unknown',
})
})
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server listening on port ${PORT}`)
})
// Graceful shutdown — important for Nomad rolling deploys
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully')
process.exit(0)
})