Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS How to organize page navigation correctly?

dt65231

Coder
How to organize page navigation correctly?
Problem: PageOneOne page: - go to PageOne - does not work (I get a link: “/pageone/pageone". Error 404);

Index page:
go to PageOne - it works;

PageOne page:
go to PageOne - it works;
go to Index - it works;

PageOneOne page:
go to PageOne - does not work(!!!) (I get the link: “/pageone/pageone". Error 404);
go to Index - works;

16.08.2022_20-11-35.png

app.js
JavaScript:
// Code --- ---- --- --- --- --- ---
const indexRoutes = require('./routes/index.routes');
const pageOneRoutes = require('./routes/pageone.routes');
const pageOneOneRoutes = require('./routes/pageoneone.routes');
// Code --- ---- --- --- --- --- ---
// routes for the app
app.use('/', indexRoutes);
app.use('/pageone', pageOneRoutes);
app.use('/pageone/pageoneone', pageOneOneRoutes);
// Code --- ---- --- --- --- --- ---

routes​

index.routes.js
JavaScript:
const express = require("express");
const router = express.Router();
const indexController = require('../controllers/index.controller');
router.get('/', indexController.index);
module.exports = router;

pageone.routes.js
JavaScript:
const fs = require('fs');
const express = require("express");
const router = express.Router();
const pageoneController = require('../controllers/pageone.controller');
router.get('/', pageoneController.index);
module.exports = router;

pageoneone.routes.js
JavaScript:
const fs = require('fs');
const express = require("express");
const router = express.Router();
const pageoneoneController = require('../controllers/pageoneone.controller');
router.get('/', pageoneoneController.index);
module.exports = router;

controllers
---
index.controller.js
JavaScript:
const fs = require('fs');
exports.index = (req, res) => { 
    res.render('index.ejs', {   
        title: "Welcome to View `Index`",       
    });
    console.log(`Controller "index.controller" method: index`) // My
};


pageone.controller.js
JavaScript:
const fs = require('fs');
exports.index = (req, res) => {
       console.log(`Controller "pageOne.controller" method: "index"`);
       return res.render('pageone.ejs');     
};


pageoneone.controller.js
JavaScript:
exports.index = (req, res) => {
       console.log(`Controller "pageOneOne.controller" method: "index"`);
       return res.render('pageoneone.ejs');     
};

views​

index.ejs
HTML:
<h1>Index page</h1>
<a href="pageone" >`PageOne`</a> </br>

pageone.ejs
HTML:
<h1>PageOne</h1>
<a href="pageone/pageoneone" >`PageOneOne`</a> </br>
<a href="/" >`Index`(Главная)</a> </br>

pageoneone.ejs
HTML:
<h1>PageOneOne</h1>
<a href="pageone" >-=PageOne=-</a> </br>
<a href="/" >`Index`(Главная)</a> </br>



app.js. All code.
----
JavaScript:
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const path = require('path');

const app = express();

const indexRoutes = require('./routes/index.routes');
const pageOneRoutes = require('./routes/pageone.routes');
const pageOneOneRoutes = require('./routes/pageoneone.routes');
//
const port = 2000;

// create connection to database
// the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name.
const db = mysql.createConnection ({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'socka'
});

// connect to database
db.connect((err) => {
    if (err) {
        throw err;
    }
    console.log('Connected to database');
});
global.db = db;

// configure middleware
app.set('port', process.env.port || port);                  // set express to use this port
app.set('views', __dirname + '/views');                     // set express to look in this folder to render our view
app.set('view engine', 'ejs');                              // configure template engine
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());                                 // parse form data client
app.use(express.static(path.join(__dirname, 'public')));    // configure express to use public folder
app.use(fileUpload());                                      // configure fileupload

// routes for the app
app.use('/', indexRoutes);
app.use('/pageone', pageOneRoutes);
app.use('/pageone/pageoneone', pageOneOneRoutes);

// Error
app.get('*', function(req, res, next){
    res.status(404);
    res.render('404.ejs', {
        title: "Page Not Found",
    });

})


// set the app to listen on the port
app.listen(port, () => {
    console.log(`Server running on port: ${port}`);
});


16.08.2022_20-28-50.png
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom