Search code examples
node.jsexpressmiddleware

Express middleware issue callback issue


I got this middleware.

const validateInput = (req, res, next) => {
 return console.log(req.body);
};

When I run my server I get this error:

Error: Route.post() requires a callback function but got a [object Undefined]

If I change the middleware to return a function like so:

const validateInput = (req, res, next) => {
 return () => console.log(req.body);
};

It runs but the console.log print undefined.

What am I missing about express rules here? (To be clear, the middleware before this one does have a next() that should pass req)


Solution

  • Why did you return? if you could give route section that would be great to debug.

    if you use like this?

      const middleware = (req, res, next) => {
        console.log(req.body)
        next();
      };
      route.get('/' middleware, controller);