Skip to content

Middleware implementation #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add logger Middleware
  • Loading branch information
Jackyrd3 committed Mar 30, 2024
commit 0ee2da7b9fc7513bd2201dcd3d0cb444ef865668
32 changes: 32 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Import required modules
const express = require('express');

// Create an Express application
const app = express();

// Middleware function to log requests
app.use((req, res, next) => {
console.log(`Received a ${req.method} request to ${req.url}`);
next(); // Call next() to move to the next middleware or route handler
});

// Middleware function to check if the request contains a specific header
app.use((req, res, next) => {
if (req.headers.authorization) {
console.log('Authorization header present');
} else {
console.log('Authorization header not present');
}
next();
});

// Route handler
app.get('/', (req, res) => {
res.send('Hello, World!');
});

// Starting the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});