Node, Express - CANNOT GET route

23,637

Solution 1

let me quote from express doc:

A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on. see express doc

this is "not working" because you set the /about in the app.use and in the router.get. try to request /about/about and you will see that this is working (just not as you wanted to)..

now just change the /about in the routes/about.js then rerun and try to request /about and it will work :)

Solution 2

Your route is set to /about/about. Change about.js to this:

const express = require('express');
const router = express.Router();

router.get('/', function(req, res) {
   res.render('app/about');
});

module.exports = router;
Share:
23,637
Jared
Author by

Jared

Senior Software Engineer at Casper

Updated on July 05, 2022

Comments

  • Jared
    Jared almost 2 years

    I am building an Express app and having some issues with routing. My '/' route is working perfectly, however other routes are not. I've looked into other questions people have posted and these have not resolved my issues.

    I have a routes/index.js file:

    module.exports = function(app){
      app.use('/', require('./routes/home'));
      app.use('/about', require('./routes/about'));
    }
    

    My routes/home.js: - WORKING!

    const express = require('express');
    const router = express.Router();
    
    router.get('/', function(req, res) {
       res.render('app/home');
    });
    
    module.exports = router;
    

    My routes/about.js: - NOT WORKING!

    const express = require('express');
    const router = express.Router();
    
    router.get('/about', function(req, res) {
       res.render('app/about');
    });
    
    module.exports = router;
    

    When I go to '/about' I see this error in the browser - 'Cannot GET /about'

    Both the home.html and about.html files are located in the same views directory.

    Any help here would be very appreciated!

  • Jared
    Jared over 7 years
    That did it! Thanks so much for the assistance.
  • Jared
    Jared over 7 years
    Yes I see okay! Thank you. : )
  • Jamon Holmgren
    Jamon Holmgren over 4 years
    Thank you! This was very helpful!