Skip to content

Middleware does not work for a filter #48

Description

@titovanton

The problem is even deeper than i've described in the caption. Lets say i have this code:

const get = Picker.filter(function(req, res) {
  return req.method == 'GET';
});
const post = Picker.filter(function(req, res) {
  return req.method == 'POST';
});

// Hello there, you have no documentation provided about custom middleware, but it's ok, i can use google
const someMiddleware = (req, res, next) => {
  console.log('see this on any route, lol');
  next();
}

// i would use the middleware only for POST requests, so i suppose i should write this way:
post.middleware(someMiddleware);

// no, u didn't get it, i even expect the middleware is gonna be run only for this route: /api/items/
// just notice: why did u splited out the params from the req object? The Express does not, so it makes some mess, when i want to assign some URL/HEADER/BODY encoded parameters to some local variable, but the word "params" is already busy
post.route('/api/items/', callback);

// and lets say i want to provide some SEO markup for google/tweeter/blablaElse bots with this one:
const seo = Picker.filter(function(req, res) {
  return req.method == 'GET';
});

seo.get('/', seoCallback);

Ok, what do we have? Unexpected behavior for a normal mind flow:
'see this on any route, lol' - u will see this on every request, does not metter, did u come to '/', or '/api/items' with POST header. Filter also does not make effect on a decision to launch or not to launch a middleware. So all code above makes absolutly NO sense! You can not validate some header encoded parameter, with redirect on fail or next() on success, in middleware for specific route of a specific filter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions