gpt4 book ai didi

javascript - Node.js MVC 路由问题

转载 作者:行者123 更新时间:2023-12-03 06:08:35 25 4
gpt4 key购买 nike

我正在 Node.js 中实现 MVC 模式,并按如下所述进行设置。我在 localhost 上得到一个页面,该页面只是卡在我的 console.log 中,因为我有一个记录器。

app.js

var express  = require("express");
var partials = require("express-partials");
var cookies = require("cookie-parser");
var session = require("express-session");
var uuid = require("node-uuid");
var parser = require("body-parser");
var csrf = require("csurf");
var compress = require("express-html-minify");

var logger = require("./controllers/logger");
var errors = require("./controllers/errors");
var utility = require("./controllers/utility");

var routes = require("./routes");

var app = new express();
var port = 80;

app.set("view engine", "ejs");
app.set("views", __dirname + "/views");
app.set("view options", {defaultLayout: "layout"});

app.use(express.static(__dirname + "/public"));

app.use(logger.log);
app.use(partials());
app.use(cookies());
app.use(session({secret: uuid.v1(), resave: true, saveUninitialized: true}));
app.use(parser.json());
app.use(parser.urlencoded({extended: false}));
app.use(csrf());
app.use(utility.csrf);
app.use(compress);
app.use(routes.controller);

app.use(errors.error);
app.use(errors.notfound);

app.listen(port);
console.log("app listening on port " + port);

./routes/index.js

var express = require("express");

var app = express();

var controllers = require("../controllers");

exports.controller = function() {
app.get("/", controllers.index);
app.get("/login", controllers.login);
app.post("/login", controllers.process);
app.get("/reset", controllers.reset);
app.get("/whoops", controllers.whoops);
};

./controllers/index.js

exports.index = function(request, response) {
response.render("index", {layout: "layout", title: "Index", cookie: JSON.stringify(request.cookies), session: JSON.stringify(request.session)});
};

exports.login = function(request, response) {
response.render("login", {layout: "layout", title: "Login", cookie: JSON.stringify(request.cookies), session: JSON.stringify(request.session)});
};

exports.process = function(request, response) {
response.render("process", {layout: "layout", title: "Process", username: request.body.username});
};

exports.reset = function(request, response) {
response.render("reset", {layout: "layout", title: "Reset"});
};

exports.whoops = function(request, response) {
response.render("whoops", {layout: "layout", title: "Whoops"});
};

最佳答案

需要进行一些更改才能解决路由器问题。

文件“app.js”更改:-

1) 如下所述更改路由,但不强制在路径中包含索引。这样您就可以清楚地引用索引文件。

var routes   = require("./routes/index");

2) 将“app.use(routes.controller)”代码替换为以下代码

app.use('/', routes);

文件“routes/index.js”更改:-

替换文件内容如下:-

现在,请求将根据 URL 路由到“controllers/index.js”中的相应方法。

var express = require("express");
var router = express.Router();

var controllers = require("../controllers/index");
router.get("/", controllers.index);
router.get("/login", controllers.login);
router.post("/login", controllers.process);
router.get("/reset", controllers.reset);
router.get("/whoops", controllers.whoops);

module.exports = router;

关于javascript - Node.js MVC 路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39425016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com