Я работаю над проектом, использующим экспресс-рули, но при попытке рендеринга после входа в систему команда res.render ("панель мониторинга") просто возвращается в ответ as. server.js
const mongoose = require("mongoose");
const cookieSession = require('cookie-session');
const expresshb = require("express-handlebars");
const passport = require("passport");
const bodyParser = require("body-parser");
require("dotenv").config();
require("./models/users");
require("./config/passport");
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/fnf_db";
mongoose.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const app = express();
app.use(
cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [process.env.cookieKey]
})
);
app.use(passport.initialize());
app.use(passport.session());
//console.log(process.env.AWS_ACCESS_ID_KEY);
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
//expresshb({ defaultLayout: "main" })
const hbs = expresshb.create({ /* config */ });
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
app.enable('view cache');
const PORT = process.env.PORT || 3000;
const apiaAuthRoute = require("./routes/apiAuthRoutes");
const apiPostRoutes = require("./routes/apiPostRoutes");
//const apiProductImages = require("./routes/apiProductImages");
//const apiGetRoutes = require("./routes/apiGetRoutes");
const htmlRoutes = require("./routes/htmlRoutes");
app.use(apiPostRoutes);
app.use(apiaAuthRoute);
app.use(htmlRoutes);
app.listen(PORT, function () {
console.log("App listening on PORT: " + PORT);
});
/// это код, который делает запрос от клиента
$("#login-button").on("click", function (event) {
event.preventDefault();
const email = $("#login-email").val();
const password = $("#login-password").val();
const user = { email, password };
console.log("posting");
console.log(user);
$.get("/api/login", user, function (userResult) {
console.log(userResult);
$("#login-email").val("");
$("#login-password").val("");
$("#Modal-login").modal("toggle");
if (userResult) {
// $.get("/dashboard");
}
else {
// call modal error
//console.log("user not found");
errorModal(userResult.error);
}
});
});
/// это код, который принимает запрос
const router = require("express").Router();
const passport = require("passport");
router.get("/api/login", passport.authenticate("local"), function (req, res) {
console.log("after post");
console.log(req.user);
//res.json({ success: true, msg: "login succes" });
res.redirect("/dashboard");
});
// google auth
router.get('/auth/google', passport.authenticate('google', {
scope: ['profile', 'email']
})
);
router.get(
'/auth/google/callback',
passport.authenticate('google'),
(req, res) => {
//switch to render dashboard
//console.log("callback data");
//console.log(req.user);
res.redirect("/dashboard");
}
);
router.get('/api/logout', (req, res) => {
req.logout();
console.log("loggedout");
res.redirect('/');
});
router.get('/api/current_user', (req, res) => {
res.send(req.user);
});
module.exports = router;
/// И вот куда он перенаправляется
const router = require("express").Router();
const passport = require("passport");
router.get("/api/login", passport.authenticate("local"), function (req, res) {
console.log("after post");
console.log(req.user);
//res.json({ success: true, msg: "login succes" });
res.redirect("/dashboard");
});
// google auth
router.get('/auth/google', passport.authenticate('google', {
scope: ['profile', 'email']
})
);
router.get(
'/auth/google/callback',
passport.authenticate('google'),
(req, res) => {
//switch to render dashboard
//console.log("callback data");
//console.log(req.user);
res.redirect("/dashboard");
}
);
router.get('/api/logout', (req, res) => {
req.logout();
console.log("loggedout");
res.redirect('/');
});
router.get('/api/current_user', (req, res) => {
res.send(req.user);
});
module.exports = router; ```