-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day07.js
31 lines (24 loc) · 897 Bytes
/
Day07.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
// Problem: Express Middleware
// Problem Statement: Implement an Express middleware function that logs the timestamp and the HTTP method of every incoming request to the server.
const express = require('express');
const app = express();
/**
* Express middleware to log incoming requests
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next function
*/
function requestLoggerMiddleware(req, res, next) {
// Get the current timestamp
const timestamp = new Date().toLocaleString();
console.log(`${timestamp} - ${req.method} request received.`);
next();
}
app.use(requestLoggerMiddleware);
app.get('/', (req, res) => {
res.send('Day 7 successfully completed!!');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port http://localhost:${PORT}`);
});