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

后端 未结 9 1302
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 20:30

There have been some middleware changes on the new version of express and I have made some changes in my code around some of the other posts on this issue but I can\'t get a

相关标签:
9条回答
  • 2020-11-29 21:25

    I was getting the same error message but had a different issue. Posting for others that are stuck on same.

    I ported the get, post, put, delete functions to new router file while refactoring, and forgot to edit the paths. Example:

    Incorrect:

    //server.js
    app.use('/blog-posts', blogPostsRouter);
    
    //routers/blogPostsRouter.js
    router.get('/blog-posts', (req, res) => {
      res.json(BlogPosts.get());
    });
    

    Correct:

    //server.js
    app.use('/blog-posts', blogPostsRouter);
    
    //routers/blogPostsRouter.js
    router.get('/', (req, res) => {
      res.json(BlogPosts.get());
    });
    

    Took a while to spot, as the error had me checking syntax where I might have been wrapping an argument in an object or where I missed the module.exports = router;

    0 讨论(0)
  • 2020-11-29 21:30

    In any one of your js pages you are missing

    module.exports = router;
    

    Check and verify all your JS pages

    0 讨论(0)
  • 2020-11-29 21:30

    You are missing router exports module and that is the reason why this error is present.

    use module.exports = router; and that would work

    0 讨论(0)
提交回复
热议问题