Node.js / Expess.js无法识别静态路由。

3
我有以下设置(由app.js调用),但似乎无法让Node.js / Express.js识别静态路由。文件存在,但仍然收到404错误。
我错过了什么?我看到一些人的静态声明在stylus之前,但这不是问题所在。此外,即使是实际存在于我的计算机上的js文件也返回404错误。
谢谢!
var fs = require('fs')
  , express = require('express')
  , basicAuth = require('express').basicAuth
  , stylus = require('stylus')
  , csrf = require('express-csrf')
  , gzippo = require('gzippo')
  , canSecurity = require('cansecurity')
  , emessages = require('express-messages')
  , dateformat = require('./lib/dateformat')
  , csauth = require('./cs/auth')
  , cansec;

  cansec = canSecurity.init({
     getUser: csauth.getUser
   , validatePassword: csauth.validatePassword 
   , sessionKey: 'secret'
   , fields: {id: "_id"}
}); 
exports.boot = function(app){
  bootApplication(app);
  bootErrorConfig(app);
};
function bootApplication(app) {
  app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.set('view options', { layout: 'layouts/default' });
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({ secret: 'secret' }));
    app.use(express.logger(':method :url :status'));
    app.use(express.favicon());
    app.use(cansec.validate);
    app.use(app.router);
  });
  app.dynamicHelpers({
    request: function(req){
      return req;
    },
    hasMessages: function(req){
      if (!req.session) return false;
      return Object.keys(req.session.flash || {}).length;
    },

    messages: emessages,

    dateformat: function(req, res) {
      return dateformat.strftime;
    },

    csrf: csrf.token
  });


  function compile(str, path) {
    return stylus(str)
      .set('filename', path)
      .set('warn', true)
      .set('compress', true)
      .define('img', stylus.url({ paths: [__dirname + '/public/images'], limit:1000000 }));
  };
  app.use(stylus.middleware({
      debug: true
    , src: __dirname + '/stylus'
    , dest: __dirname + '/public'
    , compile: compile
  }));

  app.use(express.static(__dirname + '/public'));
  app.set('showStackError', false);
  app.configure('development', function(){
    app.set('showStackError', true);
  });
  app.configure('staging', function(){
    //app.use(gzippo.staticGzip(__dirname + '/public', { maxAge: oneYear }));
    //app.enable('view cache');
  });
  app.configure('production', function(){
    //app.use(gzippo.staticGzip(__dirname + '/public', { maxAge: oneYear }));
  });
  app.use(csrf.check());
}
function bootErrorConfig(app) {
  app.use(function(req, res, next){ next(new NotFound(req.url)); });
  function NotFound(path){
    this.name = 'NotFound';
    if (path) {
      Error.call(this, 'Cannot find ' + path);
      this.path = path;
    } else {
      Error.call(this, 'Not Found');
    }
    Error.captureStackTrace(this, arguments.callee);
  }
  NotFound.prototype.__proto__ = Error.prototype;
  app.error(function(err, req, res, next){
    if (err instanceof NotFound){
      console.log(err.stack);
      res.render('404', {
        layout: 'layouts/default',
        status: 404,
        error: err,
        showStack: app.settings.showStackError,
        title: 'Oops! The page you requested desn\'t exist'
      });
    }
    else {
      console.log(err.stack);
      res.render('500', {
        layout: 'layouts/default',
        status: 500,
        error: err,
        showStack: app.settings.showStackError,
        title: 'Oops! Something went wrong!'
      });
    }
  });

  appName = 'test'

 app.get('/', function(req, res){
    res.redirect('/register');
  });

}

我喜欢你将应用程序配置和路由包装到一个启动函数中的方式。你在哪里调用boot()函数? - qodeninja
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接