Skip to content
Advertisement

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:

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 🙂

Advertisement

Answer

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");
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement