Node.js and express : Routes

后端 未结 1 432
栀梦
栀梦 2021-01-25 03:34

I have some trouble using the router from Express. I want to set up my routes with several files. I get my routes folder with 2 files: routes.js and inscription.js

I do

相关标签:
1条回答
  • 2021-01-25 03:55

    This route router.get('/inscription', ...) in your inscription router is configured for the route /inscription/inscription which is likely not what you intended. This is because you've specified it in two places:

    app.use('/inscription', inscription);
    router.get('/inscription', ...)
    

    So, the whole router is on /inscription from the app.use('/inscription', inscription). That means that any route the router itself defines will be added to that path.

    It isn't exactly clear from your question exactly what you intend for the URLs to be. But, if you just want the above router.get() to work for a /inscription URL, then change:

    router.get('/inscription', ...)
    

    to:

    router.get('/', ...)
    

    When you use app.use('/inscription', inscription);, every single route in that router will be prefixed with /inscription. So, this route:

    router.post('/adduser', ...)
    

    will be mounted at:

    /inscription/adduser
    

    Or, if you want all the inscription routes to be at the top level too, then change:

    app.use('/inscription', inscription);
    

    to this:

    app.use('/', inscription);
    

    So that nothing is added to the path beyond what the router itself defines.

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