-2

I'm using the ExpressJS web framework for NodeJS.

I don't know why to have 404 error messages when creating a new page and call it.

My directory structure:

| my-application
    | src
        | routes
            | index.js
            | single.js
        | views
            | index.pug
            | single.pug
        | ...

Code single.js in routes folder:

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

/* GET single page. */
router.get('/single', function(req, res, next) {
  res.render('single', { title: 'Blog single' });
});

module.exports = router;

Code in app.js:

// ...
var index = require('./src/routes/index');
var single = require('./src/routes/single');

app.use('/', index);
app.use('/single', single);
// ...
  • 2
    Welcome to SO. Please read this [how-to-ask](http://stackoverflow.com/help/how-to-ask) and follow the guidelines there to refine your question with additional information, such as code and error message to describe your programming problem. – thewaywewere Jun 10 '17 at 12:16
  • Don't post code as images... copy it hear – jAC Jun 10 '17 at 12:16

1 Answers1

0

This line of code:

app.use('/single', single);

along with the single router creates a route handler for /single/single, not for just /single. It registers a router to handle all /single routes and then that router itself registers /single creating the handler for /single/single.

You can either change the router.get() to handle just / instead of /single. or you can change the app.use() to not have a path. You probably want to change the router.

jfriend00
  • 683,504
  • 96
  • 985
  • 979