const express = require('express');
const app = express();
const port = 3000;
// Middleware to parse JSON bodies (not needed for current routes)
app.use(express.json());
// Hit counters
const hitCounters = {
'/': 0,
'/test1': 0
};
// Helper function to generate HTML
function generateHtml() {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hit Counter</title>
<style>
table {
width: 50%;
margin: 20px auto;
border-collapse: collapse;
}
th, td {
padding: 10px;
border: 1px solid #ddd;
text-align: center;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1 style="text-align: center;">Hit Counter</h1>
<table>
<tr>
<th>Route</th>
<th>Hits</th>
</tr>
<tr>
<td>/</td>
<td>${hitCounters['/']}</td>
</tr>
<tr>
<td>/test1</td>
<td>${hitCounters['/test1']}</td>
</tr>
</table>
</body>
</html>
`;
}
// Root route
app.get('/', (req, res) => {
hitCounters['/']++;
console.log(`Activity on server: / route accessed ${hitCounters['/']} times`);
res.send(generateHtml());
});
// Test1 route
app.get('/test1', (req, res) => {
hitCounters['/test1']++;
console.log(`Activity on server: /test1 route accessed ${hitCounters['/test1']} times`);
res.send(generateHtml());
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});