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

181
Views
Cannot get my routes that have been defined

I am trying to add routes to my expressjs project. I am trying to make it so that when I go to 'localhost:9000/users' it returns 'User List'. Currently it shows ,'Cannot GET /users'. I have tried putting the code in users.js into server.js and replaced the router with app but that did not work.
routes/users.js:

const express = require("express")
const router = express.Router()

router.get("/", (req, res) => {
  res.send("User List")
})

router.get("/new", (req, res) => {
  res.render("users/new")
})

module.exports = router;

server.js:

const express = require('express')
const app = express()
const port = 9000
const userRouter = require("routes/users")
app.set('view engine', 'ejs')

app.get('/', (req, res) => {
  res.render('index', {username: 'xpress'})
})

app.use("/users", userRouter)

app.listen(port, () => {
  console.log(`App listening at port ${port}`)
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I copy your code and only modify

const userRouter = require("routes/users")

to

const userRouter = require("./routes/users")

and I have the desired output

enter image description here

You should use "./" to refer to your files. Otherwise, Node.js will search for package "routes/users" in built-in packages, globally-installed packages, and in node_modules folder. Of course, it doesn't available in these places.

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!