我的Express Node.js应用程序路由有时会抛出404错误

时间:2019-07-26 17:49:41

标签: node.js postgresql express

更新工作时的重现步骤: 1.从/about/<%= about.id %>/edit路线中以“ submit / update“ method =” post“>形式点击路线 2.在表单文本字段中输入几个单词 3.单击提交 4.它会按预期更新

在更新不起作用时重现的步骤: 1.从/about/<%= about.id %>/edit路线中以“ submit / update“ method =” post“>形式点击路线 2.在表单文本字段中输入3行以上的文本 3.单击提交 4. 404错误返回,并显示以下消息:未找到。重定向到/ about / 3

我检查了我的验证代码,没有最大字符数限制会导致此错误。

我尝试重构代码,并调整app.js文件中app.use和app.required行的顺序。

这是我的回购,以防我未包含足够的代码-https://github.com/judypatoodie/browsByChia

我的主配置文件:

require("dotenv").config();
const path = require("path");
const viewsFolder = path.join(__dirname, "..", "views");
const bodyParser = require("body-parser");
const passportConfig = require("./passport-config");
const expressValidator = require("express-validator");
const session = require("express-session");
const flash = require("express-flash");

module.exports = {
  init(app, express){
       app.set("views", viewsFolder);
       app.use(bodyParser.urlencoded({ extended: true }));
       app.use(expressValidator());
       app.set("view engine", "ejs");
       app.use(express.static(path.join(__dirname, "..", "assets")));
       app.use(session({
         secret: process.env.cookieSecret,
         resave: false,
         saveUninitialized: false,
         cookie: { maxAge: 1.21e+9 } //set cookie to expire in 14 days
       }));
       app.use(flash());
        passportConfig.init(app);
       app.use((req,res,next) => {
         res.locals.currentUser = req.user;
         next();
       })
  }
};

我的路由配置文件:

module.exports = {
  init(app){
    const staticRoutes = require("../routes/static");
    const aboutRoutes = require("../routes/about");
    const userRoutes = require("../routes/users");


    app.use(aboutRoutes);
    app.use(staticRoutes);
    app.use(userRoutes);
  }
}

我的app.js文件:

const express = require("express");
const app = express();
const appConfig = require("./config/main-config.js");
const routeConfig = require("./config/route-config.js");


appConfig.init(app, express);
routeConfig.init(app);



module.exports = app;

我的src / routes / about.js文件:

const express = require("express");
const router = express.Router();
const helper = require("../auth/helpers");
const validation = require("./validation");
const aboutController = require("../controllers/aboutController");


//router.get("/about", aboutController.index);
router.get("/about/new", aboutController.new);
router.post("/about/create", aboutController.create);
router.get("/about/:id", aboutController.show);
router.get("/about/:id/edit", aboutController.edit);
router.post("/about/:id/update", aboutController.update);
module.exports = router;

预期结果:

  1. /about/<%= about.id %>/edit路由中以此表单的形式提交/更新“ method =” post“>路由
  2. 根据需要键入尽可能多的文本行
  3. 点击提交
  4. 它会按预期更新

0 个答案:

没有答案