所以我的 ExpressJS 应用程序有以下开发配置:
//core libraries
var express = require('express');
var http = require('http');
var path = require('path');
var connect = require('connect');
var app = express();
//this route will serve as the data API (whether it is the API itself or a proxy to one)
var api = require('./routes/api');
//express configuration
app.set('port', process.env.PORT || 3000);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.errorHandler({
dumpExceptions: true, showStack: true
}));
app.use(connect.compress());
//setup url mappings
app.use('/components', express.static(__dirname + '/components'));
app.use('/app', express.static(__dirname + '/app'));
app.use(app.router);
require('./api-setup.js').setup(app, api);
app.get('*', function(req, res) {
res.sendfile("index-dev.html");
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
现在您可以看到我正在做app.use('/components', express.static(__dirname + '/components'));
但是如果我尝试使用 /components 路径加载一个文件并且它不存在,它正在加载我想要 404 错误的 index-dev.html。有没有办法修改:
app.get('*', function(req, res) {
res.sendfile("index-dev.html");
});
这样它将为已设置但找不到文件的静态路径返回 404,如果路径不是静态路径之一,则返回 index-dev.html?