0
0
Expressframework~20 mins

req.method and req.url in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Using req.method and req.url in Express
📖 Scenario: You are building a simple Express server that logs the HTTP method and URL of incoming requests. This helps you understand what requests your server receives.
🎯 Goal: Create an Express server that listens on port 3000 and logs the req.method and req.url for every request.
📋 What You'll Learn
Create an Express app using express()
Use app.use to add a middleware that logs req.method and req.url
Start the server listening on port 3000
Use exact variable names: app, req, res, next
💡 Why This Matters
🌍 Real World
Logging request methods and URLs helps developers monitor and debug web servers by seeing what requests come in.
💼 Career
Understanding req.method and req.url is essential for backend developers working with Express to handle routing and middleware.
Progress0 / 4 steps
1
Setup Express app
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express().
Express
Need a hint?

Use require('express') to import Express and then call it to create the app.

2
Add logging middleware
Use app.use to add a middleware function with parameters req, res, and next. Inside it, write a line that logs req.method and req.url using console.log. Then call next().
Express
Need a hint?

The middleware function receives req, res, and next. Use console.log(req.method, req.url) to log the method and URL.

3
Add a simple route
Add a GET route for path '/' using app.get. The handler function should take req and res and respond with res.send('Hello World').
Express
Need a hint?

Use app.get to create a route for '/' that sends back 'Hello World'.

4
Start the server
Use app.listen to start the server on port 3000. The callback function should log 'Server running on port 3000'.
Express
Need a hint?

Use app.listen(3000, () => { console.log(...) }) to start the server and log a message.