Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da9bc1e

Browse files
Merge pull request #25 from Jackyrd3/middleware-implementation
Middleware implementation
2 parents 567e13d + a009945 commit da9bc1e

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

‎loggerMiddleware.js

Lines changed: 29 additions & 0 deletions
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+
});

‎server.js

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /