Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot GET routes other than "/" express

I’m trying to create a web app with the MVC pattern and express in node.js. This is my project structure:
project structure image

app.js:

let express = require("express");
let hbs = require("hbs");
let app = express();

let indexController = require("./controllers/indexController");
let loginController = require("./controllers/loginController");

app.set("view engine", "hbs");

app.use('/login', loginController);
app.use('/', indexController);

app.listen(3000);

loginController.js:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let express = require("express");
let router = express.Router();

router.get('/login', (req, res) => {
    res.render("login");
});

module.exports = router;

The problem is that I can access the index page ("/") but not the login page ("/login") even when the code for both is almost identical.

Here’s the code of indexController.js:

let express = require("express");
let router = express.Router();

router.get('/', (req, res) => {
    res.render("index");
});

module.exports = router;

I don’t really know what’s happening, I’m kinda new to express and MVC and couldn’t find anything online, please help 🙂

>Solution :

You have ‘login’ twice. The first one is enough in app.use('/login', loginController); Inside, you can remove the other

router.get('/', (req, res) => {
    res.render("login");
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading