Reputation: 5355
In my users
route file I am parsing the following route:
router.get("/login", (req, res) => {
res.render('login')
})
module.exports = router
In my app.js
file I am loading the following routes:
app.use("/", posts)
app.use("/new", posts)
app.use("/login", users)
app.use("/logout", users)
app.use("/register", users)
When I am opening http://localhost:8080/login
I am getting back:
Cannot GET /login
However, in my posts
router my routes are working fine and loading correctly in the browser:
router.get("/", (req, res) => {
service.getAllPosts(function(result) {
res.render("index", {
postList: result,
})
})
})
module.exports = router
Any suggestions where this error is coming from?
Upvotes: 0
Views: 41
Reputation: 707606
Your route definitions are messed up in a bunch of ways.
Assuming users
is a route, you've registered the same router multiple times. You never want to do that. This has the same router in the router chain multiple times which is not what you want. A router should only be registered once.
You've registered a router for /login
and then registered a route on that router for /login
. That creates a route handler for /login/login
.
To fix, you need to change how your router is registered and then how the routes on it are defined. You should only use a path with the app.use(path, router)
if all the routes on your router intend to have a common top level path segment. If not, then don't use anything other than the /
path with the app.use()
.
You don't show enough of your router definition to know for sure what you intend, but if you're just trying to create a router than handles /login
, /logout
and /register
, then you can just change your app.use()
from this:
app.use("/login", users)
app.use("/logout", users)
app.use("/register", users)
to this:
app.use("/", users);
This will gives your users router a chance to see all top level path segments.
And, depending upon what posts
is, you may also need to make sure that line of code is before this one:
app.use("/", posts);
Note, it is a little unusual to use multiple routers for different routes all at the same path level. While it can be made to work, it's not typically the most intuitive way to design things. Usually, you would put all routes on a subpath on the same router, but not split multiple routes at the same subpath across different routers. Note, you don't have to use different routers just to spread your routes among different files.
Upvotes: 1