Search code examples
javascriptnode.jsexpressroutesexpress-router

How to solve: Error: Route.get() requires a callback function but got a [object Object]?


Routes Folder Structure

Note: There was no problem, until I added docs_router.js
routes folder structure


docs_router.js

const { control_docs_v1, ipManager } = require('../controllers/indexer')
const router = require('express').Router()
router.get('/v1', ipManager, control_docs_v1)
module.exports = router

location.js

const { ipManager, sendData } = require('../controllers/indexer')
const router = require('express').Router()
router.get('/weather/:loc', ipManager, sendData)
module.exports = router

indexer.js

const location = require('./location')
const docs_router = require("./docs_router")

module.exports = {
    location,
    docs_router
}

In the entry file, index.js I have,

app.use("path1", location)
app.use("path", docs_router)

Is there any issue with exporting multiple router object? How to use router in righth way?

What I want to achieve

  • I want to keep the routes for specific features in separate files.
  • I want only one file to export everything from the routes folder

Solution

  • Finally solved. I made these changes.


    docs_router.js

    const { control_docs_v1, ipManager } = require('../controllers/indexer')
    const router = require('express').Router()
    router.get('/v1', ipManager.ipManager, control_docs_v1.control_docs_v1)
    module.exports = router
    

    location.js

    const { ipManager, sendData } = require('../controllers/indexer')
    const router = require('express').Router()
    router.get('/weather/:loc', ipManager.ipManager, sendData)
    module.exports = router
    

    Doing this resolved the error.