-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (71 loc) · 2.38 KB
/
index.js
File metadata and controls
84 lines (71 loc) · 2.38 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const client = require('prom-client');
const app = express();
app.use(bodyParser.json());
// MongoDB connection
const mongoUrl = process.env.MONGO_URL || 'mongodb://localhost:27017/tasksdb';
mongoose
.connect(mongoUrl)
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.error('MongoDB connection error:', err));
// Task schema and model
const taskSchema = new mongoose.Schema({
name: { type: String, required: true },
createdAt: { type: Date, default: Date.now },
});
const Task = mongoose.model('Task', taskSchema);
// Routes
app.get('/', (req, res) => {
res.send('<h1>Welcome to Namitha\'s Task Manager!</h1><p>Manage your tasks here.</p><a href="/tasks">View Tasks</a>');
});
app.get('/tasks', async (req, res) => {
try {
const tasks = await Task.find();
res.json(tasks);
} catch (err) {
res.status(500).json({ error: 'Failed to retrieve tasks' });
}
});
app.post('/tasks', async (req, res) => {
try {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: 'Task name is required' });
}
const newTask = new Task({ name });
const savedTask = await newTask.save();
res.status(201).json(savedTask);
} catch (err) {
res.status(500).json({ error: 'Failed to create task' });
}
});
// Prometheus metrics
const register = new client.Registry();
client.collectDefaultMetrics({ register });
const httpRequestCounter = new client.Counter({
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'route', 'status'],
});
register.registerMetric(httpRequestCounter);
app.use((req, res, next) => {
res.on('finish', () => {
httpRequestCounter.labels(req.method, req.path, res.statusCode).inc();
});
next();
});
app.get('/metrics', async (req, res) => {
res.set('Content-Type', register.contentType);
res.send(await register.metrics());
});
// Export the app (without starting the server)
module.exports = app;
// Start the server only if this file is executed directly
if (require.main === module) {
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`task Api App is running on http://localhost:${port}`);
});
}