Spaces:
Running
Running
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}`); | |
}); |