Difficulties to make another route expressJS

Issue

I am doing a site as of my studies.
The technology being free, I decided to code with nodejs/Express.
For my first route /home, everything went well. But I can’t create others.

error message

Yet I thought I understood the system well.
It would bother me if it was due to a silly error because I have been on this problem for too long 😋.

index.js :

const express = require('express');
const app = express();
app.set('view engine', 'ejs');

const PORT = process.env.PORT || 4242;

app.use('/', require('./routes/home_route'));
app.use('/auther', require('./routes/auther_route')); //the problem 😡    
app.listen(PORT, () => {
    console.log(`localhost:${PORT}`);
});

app.use('/styles', express.static(__dirname + '/styles'));
app.use('/scripts', express.static(__dirname + '/scripts'));
app.use('/pictures', express.static(__dirname + '/pictures'));

home_route.js :

const express = require('express');
const { homeView } = require('../controllers/home_controller');
const router = express.Router();
router.get('/home', homeView);
router.get('/', (req, res) => res.redirect('./home'));
module.exports = router;

home_controller.js :

let page = 'home_view.ejs';

const homeView = (req, res) => {
    res.render("constant_view.ejs", { page : page });
}
module.exports =  { homeView };

constant_view.ejs :

html...
<%= page %>
html...

And all this is very good.
And now this is what does not work 😭.

auther_route.js :

const express = require('express');
const { autherView } = require('../controllers/auther_controller');
const router = express.Router();
router.get('/auther', autherView);
module.exports = router;

auther_controller.js :

let page = 'auter_view.ejs';

const autherView = (req, res) => {
    res.render("constant_view", { page : page });
}
module.exports =  { autherView };

Thank you in advance for your time and your answers.

Solution

you are trying to reach the route /auther in index.js, but inside auther_route you create a route with /auther as well. So in index.js you should add /auther/auther or just change to app.use('/', require('./routes/auther_route'));

Regards

Answered By – Gabriel Asakawa

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published