2014-07-16 87 views
1

我想學習expressjs,並一直試圖找出一種方法來添加MVC結構到它。出於某種原因,當我嘗試訪問localhost:3000時出現404錯誤。在expressjs路由錯誤

我的目錄結構是:

  • 應用
    • app.js
    • routes.js
    • 控制器
    • 視圖

App.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;' 

routes.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上回購: https://github.com/sjang92/expressjs-mvc/blob/master/controllers/index.js

有誰知道它爲什麼會發生?

謝謝

回答

2

你還說響應請求您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(); 
+0

哦,這很有道理!非常感謝你,我會在5分鐘內接受你的回答 –