Reputation: 65
I created a simple application. Until I split the application code into an MVC template, everything worked. Below I present the code of my application. After going to localhost:3000/add-service you will see Cannot Get /add-service message.
Where is the problem that I unfortunately cannot solve?
app.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const expressLayouts = require('express-ejs-layouts');
const adminRoutes = require('./Routes/admin');
const shopRouters = require('./Routes/shop');
const app = express();
app.use(expressLayouts);
app.set('view engine', 'ejs');
app.set('views', './views');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/add-service', adminRoutes);
app.use(shopRouters);
app.use('/', (req, res, next) => {
res.status(404).render('./pages/404', {
title: '404 Page Not Found',
path: '',
});
});
app.listen(3000);
admin.js - this is my router file
const express = require('express');
const router = express.Router();
const bikesController = require('../controllers/bikes');
router.get('/add-service', bikesController.getAddBike);
module.exports = router;
bikes.js - this is my controller
const bikes = [];
exports.getAddBike = (req, resno) => {
res.render('../views/pages/add-service', {
path: 'pages/add-service',
title: 'Add in Service',
});
};
exports.postAddBike = (req, res) => {
bikes.push({ title: req.body.bikeModel });
res.redirect('/');
};
exports.getBikes = (req, res) => {
res.render('./pages/shop', {
model: bikes,
docTitle: 'List of Bikes',
path: '/',
title: 'Main Page',
});
};
App Tree
Upvotes: 0
Views: 331
Reputation: 944474
app.use('/add-service', adminRoutes);
You've mounted adminRoutes
at /add-service
router.get('/add-service', bikesController.getAddBike);
The only end point that service provides is /add-service
Combine them and you have the URL: /add-service/add-service
, which isn't what you are asking for.
The point of the router is that it doesn't need to know about the URLs above where it sits.
The router should just be dealing with its own section of the URL hierarchy:
router.get('/', bikesController.getAddBike);
Upvotes: 1