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;
app.js
pageone.routes.js
pageoneone.routes.js
controllers
---
index.controller.js
pageone.controller.js
pageoneone.controller.js
pageone.ejs
pageoneone.ejs
app.js. All code.
----
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;
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}`);
});