I have the following problem in my code, I have my index.js file inside my routes folder with the following code that I detail below, but still have the module.exports = router that I saw as many of the proposed solutions, it gives me the following error when compiling
throw new TypeError('Router.use() requires a middleware function but got a '+ gettype(fn))
const fs = require("fs")
const express = require("express")
const router = express.Router()
const pathRouter = `${__dirname}`
const removeExtension = (fileName) => {
return fileName.split('.').shift()
}
fs.readdirSync(pathRouter).filter((file) => {
const fileWithOutExt = removeExtension(file)
const skip = ['index'].includes(fileWithOutExt)
if (!skip) {
router.use(`/${fileWithOutExt}`, require(`./${fileWithOutExt}`)) //TODO: localhost/users
console.log('CARGAR RUTA ---->', fileWithOutExt)
}
})
router.get('*', (req, res) => {
res.status(404)
res.send({ error: 'Not found' })
})
module.exports = router