Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
grouping express routes into a single file

I trying to express routes into a single file using app.use

app.js

import express from 'express'
import testRouter from 'testRouter'

const app = express()

app.use('/testRouter', testRouter)

app.listen(3000)

testRouter.js

import express from 'express'
const router = express.Router
router.get("/page1", function (req, res){
   res.send("test")
})

export default router

using module exports is a official ways to do it and it worked

But why doesn't the router definition inside a function work like bellow?

app.js

import express from 'express'
import testRouter from 'testRouter'

const app = express()

app.use('/testRouter', function (){
   const router = express.Router()
   router.get("/page1", function (req, res){
       res.send("test")
   })
   return router
}))

app.listen(3000)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I don't believe the code inside app.use('/test/Router', function() { }) executes until a user has visited your test/Router page at least once. I'm also not sure if you can use a middleware ("use") without having an eventual terminal endpoint.

about 4 years ago · Juan Pablo Isaza Report

0

Because in the official documentation testRouter is an Object

app.use('/testRouter', Object)

You put the function

app.use('/testRouter', Functon)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!