Router.use() requires a middleware function but got a undefined
You pass your routes initialization as a middleware fuction
app.use('/user', authMiddleware, userRoutes(app));
In this line of code what userRoutes(app)
was supposed to return is function(req,res[,next]){}
Its should be like this
app.use('/user', authMiddleware, (req, res, next) => {
userRoutes(app);
next()
})
and what you do is
app.use('/user', authMiddleware, (app) => {
app.route('/user/signup').post(register);
app.route('/user/login').post(login);
})
that is wrong
There seems to be some bad logic here because from what I understand you will have to call http://localhost/user
to initialize the routes and that will not work well beacuse of express's middleware inclusion.
Again from what I understand what you are trying to do should look more like this
...
import userRoutes from './src/routes/userRoutes';
import invoicesRoutes from './src/routes/invoicesRoutes';
...
const app = express();
userRoutes(app);
invoicesRoutes(app);
and
import { register, login } from '../controllers/authController';
import authMiddleware from "./middleware";
const userRoutes = (app) => {
app.post('/user/signup', authMiddleware, register);
app.post('/user/login', authMiddleware, login);
};
export default userRoutes;