File size: 765 Bytes
4d1f385
dd67478
4d1f385
 
dd67478
4d1f385
 
dd67478
 
 
 
4d1f385
d3efcd3
dd67478
 
f21a2b1
dd67478
 
 
4d1f385
dd67478
 
 
f21a2b1
4d1f385
 
 
dd67478
d5ba34b
4d1f385
dd67478
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}`);
});