Так ... Я думаю, что, возможно, выяснил, что было не так:
В моем основном файле (приложение. js) я настроил свои маршруты в верхней части страницы (я не показывал весь свой файл, потому что я никогда не думал, что это будет необходимо (я новичок в кодировании, если вы никогда не догадались).
Мое старое приложение. js выглядело примерно так:
const express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
passportLocalMongoose = require('passport-local-mongoose'),
LocalStrategy = require('passport-local'),
User = require('./models/user');
//REQUIRE ROUTE FILES:
const exerciseRoutes = require('./routes/exercises'),
commentRoutes = require('./routes/comments'),
indexRoutes = require('./routes/index');
//TAKE NOTE OVER HERE:
app.use('/', indexRoutes);
app.use('/exercises', exerciseRoutes);
app.use('/exercises/:slug/comments', commentRoutes);
//CONFIGURATION OF mongoose, bodyParser, ejs and setting the use of public folders for CSS;
mongoose.connect("mongodb://localhost/kinetic", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
//PASSPORT CONFIG
app.use(require("express-session")({
secret: "Any message can go here apparently",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate())); //comes from passport-local-mongoose
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get('/', (req, res)=>{
res.send("HOME PAGE!")
});
app.use((req, res, next)=>{
res.locals.currentUser = req.user;
next()
});
//SET UP PORT:
app.listen(3000, ()=>{
console.log("Connected to kinetic!")
})
Все, что мне нужно было сделать, это переместить это ...
app.use('/', indexRoutes);
app.use('/exercises', exerciseRoutes);
app.use('/exercises/:slug/comments', commentRoutes);
... чуть выше моего слушателя порта / приложения и voilà
Теперь мое приложение. js Файл выглядит так:
const express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
passportLocalMongoose = require('passport-local-mongoose'),
LocalStrategy = require('passport-local'),
User = require('./models/user');
//REQUIRE ROUTE FILES:
const exerciseRoutes = require('./routes/exercises'),
commentRoutes = require('./routes/comments'),
indexRoutes = require('./routes/index');
//CONFIGURATION OF mongoose, bodyParser, ejs and setting the use of public folders for CSS;
mongoose.connect("mongodb://localhost/kinetic", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
//PASSPORT CONFIG
app.use(require("express-session")({
secret: "Any message can go here apparently",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate())); //comes from passport-local-mongoose
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get('/', (req, res)=>{
res.send("HOME PAGE!")
});
app.use((req, res, next)=>{
res.locals.currentUser = req.user;
next()
});
//MUST BE BELOW PASSPORT CONFIGURATION!
app.use('/', indexRoutes);
app.use('/exercises', exerciseRoutes);
app.use('/exercises/:slug/comments', commentRoutes);
//SET UP PORT:
app.listen(3000, ()=>{
console.log("Connected to kinetic!")
});