gpt4 book ai didi

javascript - expressjs中的路由错误

转载 作者:行者123 更新时间:2023-11-30 17:23:14 24 4
gpt4 key购买 nike

我正在尝试学习 expressjs,并且一直在想办法将 MVC 结构添加到其中。出于某种原因,当我尝试访问 localhost:3000 时出现 404 错误。

我的目录结构是:

  • 应用程序
    • 应用程序.js
    • 路由.js
    • 控制者
    • 观点
    • 等等

应用程序.js:

mymodule = {

/* initialize
* ==============================
* load dependencies & external node modules, setup engines */
initialize: function() {

// load modules
this.express = require('express');
this.path = require('path');
this.favicon = require('static-favicon');
this.logger = require('morgan');
this.cookieParser = require('cookie-parser');
this.bodyParser = require('body-parser');

// initialize main express app.
this.app = this.express();

// attach view engine to app
this.app.set('views', this.path.join(__dirname, 'views')); // view files are in /views/
this.app.set('view engine', 'jade');

// attach modules to app
this.app.use(this.favicon());
this.app.use(this.logger('dev'));
this.app.use(this.bodyParser.json());
this.app.use(this.bodyParser.urlencoded());
this.app.use(this.cookieParser());
this.app.use(this.express.static(this.path.join(__dirname, 'public'))); // static files are in /public/
},

/* setErrorHandlers
* ===============================
* set error handlers for routing */
setErrorHandlers: function() {
if (this.app == undefined) {
console.log("caught an attempt to set error handlers without initializing");
} else {

// Catch 404 Error and forward to error handler
this.app.use(function(req, res, next) {
console.log("Caught 404 Error");
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// Error Handlers

// 1) Dev Error Handler. print stack trace
if (this.app.get('env') === 'development') {
this.app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// 2) Production Error Handler

this.app.use(function(err, req,res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
}
},

/* setRouters
* ================================
* */
setRouters: function() {
// get all routes
this.routes = require('./routes').construct(this.app);

}
}

mymodule.initialize();
mymodule.setErrorHandlers();
mymodule.setRouters();

module.exports = mymodule.app;'

路由.js:

var index_controller = require('./controllers/index').controller();

module.exports.construct = function(app) {
console.log("got here");
//app.get('/', require('./controllers/index').about);
//app.get('/', index_controller.index);
app.get('/', function(req,res) {res.send("sdf");});
}

./controllers/index.js:

module.exports.controller = function() {
index_action = function(req, res) {
if (req == undefined) {
console.log("req undefined!");
} else {
console.log(req);
res.render('index', {title: 'Express'});
}
}

return {index: index_action};

我把代码放在了github repo: https://github.com/sjang92/expressjs-mvc/blob/master/controllers/index.js

有人知道为什么会这样吗?

谢谢

最佳答案

您正在添加一个普通的中间件函数,该函数在您的 app.get('/', ..); 处理程序有机会执行之前响应请求:

    this.app.use(function(req, res, next) {
console.log("Caught 404 Error");
var err = new Error('Not Found');
err.status = 404;
next(err);
});

您应该只在所有其他路由之后添加这种中间件。例如:

mymodule.initialize();
mymodule.setRouters();
mymodule.setErrorHandlers();

关于javascript - expressjs中的路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24771824/

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