-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (28 loc) · 947 Bytes
/
app.js
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
33
34
// Import required modules
const express = require('express');
const fs = require('fs');
// Create Express app
const app = express();
// Define a middleware function to log incoming requests
app.use((req, res, next) => {
const { ip, headers } = req;
const userAgent = headers['user-agent'];
const logMessage = `IP Address: ${ip}, User-Agent: ${userAgent}\n`;
// Log request information to a file
fs.appendFile('request_logs.txt', logMessage, (err) => {
if (err) {
console.error('Error writing to log file:', err);
}
});
// Call the next middleware in the chain
next();
});
// Define route for the homepage
app.get('/', (req, res) => {
res.send('<h1>Welcome to the Demo Website</h1>');
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});