Reputation: 943
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
)
Upvotes: 0
Views: 298
Reputation: 3270
You will need to use a body-parser middleware from express before you can access the body parsed version of your request body
.
Just before handling your routes.
app.js
const express = require('express');
const app = express();
app.use(express.json()); //<-- you will need express.json() middleware
app.use(require('./server/index')); //<-- here my routes middlewares are declared.
module.exports = app;
Upvotes: 0
Reputation: 614
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);
Upvotes: 1