获取页面时

时间:2019-05-23 08:00:33

标签: node.js express express-router

我一直在尝试使用路由器对我的代码进行一些标准化,但是现在我已经实现了所有功能,所以我似乎无法使其正常工作。在IDE中不会引发任何错误,但是在加载网站时,我无法获取/

App js文件

var express         = require("express"),
    app             = express(),
    bodyParser      = require("body-parser"),
    mongoose        = require("mongoose"),
    flash           = require("connect-flash"),
    passport        = require("passport"),
    LocalStrategy   = require("passport-local"),
    passportLocalMongoose = require("passport-local-mongoose"),
    Reply      = require("./models/forum"),
    Forum         = require("./models/forum"),
    User            = require("./models/user"),
    Landing            = require("./models/landing"),
    Event            = require("./models/events"),
    Book            = require("./models/books"),
    About            = require("./models/about"),
    Contact            = require("./models/contact"),
    Interview            = require("./models/interviews"),
    Gallery            = require("./models/gallery"),
    router              = new express.Router(),
    methodOverride  = require("method-override");

var aboutRoutes       = require("./routes/about"),
    bookRoutes    = require("./routes/books"),
    contactRoutes    = require("./routes/contact"),
    eventRoutes    = require("./routes/events"),
    forumRoutes    = require("./routes/forum"),
    galleryRoutes    = require("./routes/gallery"),
    interviewRoutes    = require("./routes/interviews"),
    indexRoutes         = require("./routes/index")

mongoose.connect("mongodb://localhost/fright_club");

app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride("_method"));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));

app.use(flash());

//PASSPORT CONFIG
app.use(require("express-session")({
    secret: "",
    resave: false,
    saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function(req, res, next){
    res.locals.currentUser = req.user;
    res.locals.error = req.flash("error");
    res.locals.success = req.flash("success");
    next();
});

app.use(router);
app.use("/", indexRoutes);
app.use("/about", aboutRoutes);
app.use("/books", bookRoutes);
app.use("/contact", contactRoutes);
app.use("/events", eventRoutes);
app.use("/forum", forumRoutes);
app.use("/gallery", galleryRoutes);
app.use("/interviews", interviewRoutes);


app.listen(process.env.PORT, process.env.IP, function() {
    console.log("The server has started");
});

Indexjs文件

var express = require("express");
var router = new express.Router();
var Landing = require("../models/landing");
var User = require("../models/user");
var passport = require("passport");
//var middleware = require("../middleware");

router.get("/", function(req, res){
    Landing.find({}, function(err, landing){
            if(err){
                console.log(err);
            } else {
                res.render("landing", {landings:landing});
            }
        });

});

router.get("/addLanding", function(req, res){
    res.render("addLanding");
});



router.post("/", function(req, res){
    var image = req.body.image;
    var date = req.body.eventDateTime;
    var location = req.body.eventLocation;
    var book = req.body.book;
    var newLanding = {image: image, date: date, location: location, book: book};
    Landing.create(newLanding, function(err, newlyCreated){
        if(err){
            console.log(err);
        } else {
            res.redirect("/");
        }
    });

});

router.get("/edit", function(req, res){
         Landing.findById(req.params.id, function(err, landing){
             if(err || !landing){
                 res.redirect("/back");
             } else{
             res.render("editLanding", {landing: landing});
             }
    });

});


router.put("/edit", function(req, res){
    Landing.findByIdAndUpdate(req.params.id, req.body.landing, function(err, landing){
    if(err){
        res.redirect("/");
    } else{
        res.redirect("/");
    }

    });
});

router.get("/login", function(req, res){
    res.render("login");
});

router.get("/signUp", function(req, res){
    res.render("signUp");
});

router.get("/signUp", function(req, res){
    res.render("signUp");
});

router.post("/signUp", function(req, res){
    var newUser = new User({username: req.body.username});
    User.register(newUser, req.body.password, function(err, user){
        if(err){
            req.flash("error", err.message);
            return res.render("signUp");
        }
        passport.authenticate("local")(req, res, function(){
            req.flash("success", "You have signed up. Welcome to Fright Club " + user.username);
            res.redirect("/");
        });
    });
});

router.get("/login", function(req, res){
    res.render("login");
});

router.post("/login", passport.authenticate("local", 
    {
        successRedirect: "/",
        failureRedirect: "/login"
    }), function(req, res){

});

router.get("/logout", function(req, res){
    req.logout();
    req.flash("success", "Logged you out!");
    res.redirect("/");
});



module.exports = router;

请告诉我是否需要其他任何代码,但是这些是相关文件,有人可以指导我做错什么吗?

1 个答案:

答案 0 :(得分:0)

自己修复此问题,安装了Express 3x,更新至v4,问题已解决。