0
私はNode.jsでMVCパターンを実装していますが、以下で説明するようにセットアップします。私はロガーを持っているので、ちょうどぶら下がっているlocalhost上のページと私のconsole.logの '/'を得ています。Node.js MVCルーティングの問題
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"});
};