Categories
express javascript middleware node.js

Router.use() requires a middleware function but got a undefined

I have this code in my index.js

...
import userRoutes from './src/routes/userRoutes';
import invoicesRoutes from './src/routes/invoicesRoutes';
import authMiddleware from "./src/middlewares/authMiddleware";
...
const app = express();
...
app.use('/user', authMiddleware, userRoutes(app));
app.use('/invoices', authMiddleware, invoicesRoutes(app));

my authmiddleware

const authMiddleware = (req, res, next) => {
if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === 'JWT') {
console.log(req.headers.authorization.split(' ')[0]);
next();
} else {
console.log('else');
next();
}
};
export default authMiddleware;

and one of my routes:

import { register, login } from '../controllers/authController';
const userRoutes = (app) => {
app.route('/user/signup')
.post(register);
app.route('/user/login')
.post(login);
};
export default userRoutes;

I’m getting an error:

throw new TypeError(‘Router.use() requires a middleware function but
got a ‘ + gettype(fn))
^

TypeError: Router.use() requires a middleware function but got a
undefined

What’s wrong?