Spaces:
Paused
Paused
Update app.js
#3
by
pluviouse
- opened
app.js
CHANGED
|
@@ -1,20 +1,32 @@
|
|
| 1 |
const express = require('express');
|
|
|
|
| 2 |
const app = express();
|
| 3 |
|
| 4 |
-
//
|
| 5 |
let hitCount = 0;
|
| 6 |
|
| 7 |
-
//
|
|
|
|
|
|
|
|
|
|
| 8 |
app.get('/', (req, res) => {
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
res.json({
|
| 11 |
-
message: '
|
| 12 |
-
hits: hitCount
|
|
|
|
|
|
|
| 13 |
});
|
| 14 |
});
|
| 15 |
|
| 16 |
-
//
|
| 17 |
const PORT = 7860;
|
| 18 |
app.listen(PORT, () => {
|
| 19 |
-
console.log(`Server
|
| 20 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
+
const morgan = require('morgan');
|
| 3 |
const app = express();
|
| 4 |
|
| 5 |
+
// Initialize hit counter
|
| 6 |
let hitCount = 0;
|
| 7 |
|
| 8 |
+
// Use Morgan middleware for logging
|
| 9 |
+
app.use(morgan('combined'));
|
| 10 |
+
|
| 11 |
+
// Route to count hits
|
| 12 |
app.get('/', (req, res) => {
|
| 13 |
+
const userAgent = req.headers['user-agent'];
|
| 14 |
+
|
| 15 |
+
// Increment hit only if not from fetch
|
| 16 |
+
if (!userAgent || !userAgent.toLowerCase().includes('fetch')) {
|
| 17 |
+
hitCount++;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
res.json({
|
| 21 |
+
message: 'Welcome to the hit counter!',
|
| 22 |
+
hits: hitCount,
|
| 23 |
+
userAgent: userAgent,
|
| 24 |
+
counted: !userAgent?.toLowerCase().includes('fetch')
|
| 25 |
});
|
| 26 |
});
|
| 27 |
|
| 28 |
+
// Listen on port 7860
|
| 29 |
const PORT = 7860;
|
| 30 |
app.listen(PORT, () => {
|
| 31 |
+
console.log(`Server is running on port ${PORT}`);
|
| 32 |
});
|