Skip to content
Myr

prevent incorrect automatic OPTIONS responses

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The router has automatic handling of OPTIONS based on the registered routes, but if you make an OPTIONS request for an endpoint that does not exist, then it will still return a 200 with nothing allowed. Instead, we can let the request move on down the middleware chain. This has two benefits: first, if the route was not defined and no other middleware handles it, it will return with a 404. Secondly, if multiple routers are used and a later one has the route or a custom OPTIONS defined, the first router will not respond incorrectly.

authored by Doug Patti · committed by Doug Patti · parent dc5932d · Browse files · 2 files+27 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−2 removed

  • tests:+24 added−0 removed
  • other:+3 added−2 removed

2 files+27 added−2 removed

modified:lib/router/index.js+3 added-2 removed
102 unmodified lines
103 // match route104 req.route = route = self.matchRequest(req, i);105106 // implied OPTIONS107 if (!route && 'OPTIONS' == req.method) return self._options(req, res);107 if (!route && 'OPTIONS' == req.method) return self._options(req, res, next);108109 // no route110 if (!route) return next(err);111 debug('matched %s %s', route.method, route.path);
68 unmodified lines
180 * @param {ServerResponse} res181 * @api private182 */183184Router.prototype._options = function(req, res){184Router.prototype._options = function(req, res, next){185 var path = parse(req).pathname186 , body = this._optionsFor(path).join(',');187 if (!body) return next();188 res.set('Allow', body).send(body);189};190191/**
130 unmodified lines
modified:test/app.options.js+24 added