我有以下设置(它由app.js调用)但我似乎无法让Node.js / Express.js识别静态路由。文件存在,但我仍然得到404.
我在这里缺少什么?我已经看到一些问题,人们在手写笔之前有静态声明,但事实并非如此。此外,即使是我身上的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');
});
}
答案 0 :(得分:1)