Skip to content

Commit 74beeac

Browse files
committed
Fix routing requests without method
1 parent 9bc1742 commit 74beeac

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix routing requests without method
45
* deps: body-parser@1.20.2
56
- Fix strict json error message on Node.js 19+
67
- deps: content-type@~1.0.5

lib/router/route.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ Route.prototype._handles_method = function _handles_method(method) {
6060
return true;
6161
}
6262

63-
var name = method.toLowerCase();
63+
// normalize name
64+
var name = typeof method === 'string'
65+
? method.toLowerCase()
66+
: method
6467

6568
if (name === 'head' && !this.methods['head']) {
6669
name = 'get';
@@ -103,8 +106,10 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
103106
if (stack.length === 0) {
104107
return done();
105108
}
109+
var method = typeof req.method === 'string'
110+
? req.method.toLowerCase()
111+
: req.method
106112

107-
var method = req.method.toLowerCase();
108113
if (method === 'head' && !this.methods['head']) {
109114
method = 'get';
110115
}

test/Router.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ describe('Router', function(){
6161
router.handle({ method: 'GET' }, {}, done)
6262
})
6363

64+
it('handle missing method', function (done) {
65+
var all = false
66+
var router = new Router()
67+
var route = router.route('/foo')
68+
var use = false
69+
70+
route.post(function (req, res, next) { next(new Error('should not run')) })
71+
route.all(function (req, res, next) {
72+
all = true
73+
next()
74+
})
75+
route.get(function (req, res, next) { next(new Error('should not run')) })
76+
77+
router.get('/foo', function (req, res, next) { next(new Error('should not run')) })
78+
router.use(function (req, res, next) {
79+
use = true
80+
next()
81+
})
82+
83+
router.handle({ url: '/foo' }, {}, function (err) {
84+
if (err) return done(err)
85+
assert.ok(all)
86+
assert.ok(use)
87+
done()
88+
})
89+
})
90+
6491
it('should not stack overflow with many registered routes', function(done){
6592
this.timeout(5000) // long-running test
6693

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy