Skip to content

Commit a009945

Browse files
committed
Add logger
1 parent 0ee2da7 commit a009945

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

loggerMiddleware.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const express = require('express');
2+
const app = express();
3+
4+
// Middleware function to log requests
5+
app.use((req, res, next) => {
6+
console.log(`Received a ${req.method} request to ${req.url}`);
7+
next(); // Call next() to move to the next middleware or route handler
8+
});
9+
10+
// Middleware function to check if the request contains a specific header
11+
app.use((req, res, next) => {
12+
if (req.headers.authorization) {
13+
console.log('Authorization header present');
14+
} else {
15+
console.log('Authorization header not present');
16+
}
17+
next();
18+
});
19+
20+
// Route handler
21+
app.get('/', (req, res) => {
22+
res.send('Hello, World!');
23+
});
24+
25+
// Starting the server
26+
const PORT = process.env.PORT || 3000;
27+
app.listen(PORT, () => {
28+
console.log(`Server is running on port ${PORT}`);
29+
});

0 commit comments

Comments
 (0)