Express req.method Property
What you’ll learn
- What value
req.methodprovides for each request. - How to build method-aware middleware and logs.
- How method overrides can affect request flow.
- How to validate allowed HTTP methods safely.
Usage syntax
javascript
req.method1
Method-based response in shared route
javascript
app.all('/resource', function (req, res) {
if (req.method === 'GET') return res.send('Read resource');
if (req.method === 'POST') return res.send('Create resource');
res.status(405).send('Method Not Allowed');
});2
Log request method in middleware
javascript
app.use(function (req, res, next) {
console.log('Method:', req.method, 'Path:', req.originalUrl);
next();
});❓ FAQ
It gives the HTTP request method, such as GET, POST, PUT, PATCH, or DELETE.
Typically yes for standard Node/Express request handling, e.g., GET or POST.
It is useful in shared middleware for logging, auditing, conditional checks, and method-based behavior.
Yes. Method override patterns may rewrite the effective method before route handling.
Route methods define allowed verbs, while req.method tells which verb the current request used.
Did you know?
req.method contains the incoming HTTP verb (like GET, POST, PUT, DELETE) and is useful in generic middleware logic.
4 people found this page helpful
