Spaces:
Running
Running
File size: 766 Bytes
4d1f385 840b6af 4d1f385 840b6af 4d1f385 840b6af 4d1f385 840b6af 4d1f385 840b6af 4d1f385 840b6af d5ba34b 4d1f385 840b6af 4d1f385 |
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 |
const express = require('express');
const morgan = require('morgan');
const app = express();
// Initialize hit counter
let hitCount = 0;
// Use Morgan middleware for logging
app.use(morgan('combined'));
// Route to count hits
app.get('/', (req, res) => {
const userAgent = req.headers['user-agent'];
// Increment hit only if not from fetch
if (!userAgent || !userAgent.toLowerCase().includes('fetch')) {
hitCount++;
}
res.json({
message: 'Welcome to the hit counter!',
hits: hitCount,
userAgent: userAgent,
counted: !userAgent?.toLowerCase().includes('fetch')
});
});
// Listen on port 7860
const PORT = 7860;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
}); |