1

I have app.js file where I route /api routes to api.js file:

const api = require("./src/rest/api")
......
app.use("/api", api)

I am planning to to do all routing in router.js file, so router.js file would look like this:

app.all("/api", api)
app.all("/status", status)

How can I achieve this? I tried to use app.use("/", router) in app.js file but when I receive request in router.js file, the path doesn't anymore exist.

ferakp
  • 83
  • 2
  • 9
  • It is not quite clear what u want to achieve? – MisterMonk May 03 '22 at 16:06
  • Does Express have a feature which I could use to tell it I have a router.js and I want add routes there? Is there a way I can route /api calls to api router using router.js file? – ferakp May 03 '22 at 18:28
  • please provide the desired full routes. The example above is not understandable at all. Because is is not clear what you want to achieve – MisterMonk May 04 '22 at 06:54

1 Answers1

1

main.js

const router = require('router.js')

app.use('/api', router)

router.js

const router = express.Router()

// path: /api/status
router.get('/status', (req, res, next)=>{
 // do something here
}) 

module.exports = router
MisterMonk
  • 327
  • 1
  • 9