Express js为目录中的所有静态文件获取404
Express js getting 404 for all static files in a directory
为了方便我的静态文件,我有多个目录。
我的一些静态文件在客户端目录中,一些与仪表板相关的文件在 src 目录中,所以现在我的目录结构如下
/
|
client //static files and other stuff
server //server side express, app, controller, models etc
src //other static files
我有两个 angular 应用程序,一个在客户端文件夹中,另一个在 src 文件夹中,我的服务器端路由如下 -
app.route('/app/dashboard-v1').get(function(req,res){
res.sendFile(path.join(__dirname, '../src', 'index.html'));
});
// All undefined asset or api routes should return a 404
app.route('/:url(api|img|src|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get(function (req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
所以我的第一个 angular 应用程序在 app/dashboard-v1 中,所有其他 urls 都被重定向到 app2
我正在正确获取我的应用程序 2 中的所有文件,但我在我的第二个应用程序中获取所有其他文件的 404。
现在如果我注释掉
// app.route('/:url(api|img|src|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
我从第一个应用程序的所有文件中的第二个应用程序获取 index.html 而不是 404 错误
我的快递配置如下-
if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));
app.set('appPath', path.join(config.root, 'public'));
app.use(morgan('dev'));
} else {
app.use(require('connect-livereload')());
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'client')));
app.use(express.static(path.join(config.root, 'src')));
app.set('appPath', path.join(config.root, 'client'));
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
我假设我的路线有问题。我该如何解决 ?
在第一个应用程序 (app/dashboard-v1) 的 index.html 中,我添加了
<base href="/src/" />
我的 index.html 中的所有链接都是相对的,如下是 src/index.html 中的一个块(app/dashboard-v1 url 应用程序)-
<script src="vendor/angular/angular-animate/angular-animate.js"></script>
<script src="vendor/angular/angular-cookies/angular-cookies.js"></script>
当我在浏览器中打开网络控制台时,向服务器发出的请求是这样的 -
Request URL:http://localhost:9000/src/vendor/angular/angular-animate/angular-animate.js
我在浏览器控制台中收到 404 状态码
你 运行 你的例子的 / direcotry 上的应用程序吗?
我认为您必须将 __dirname 设置为静态文件夹
app.use('/',express.static(__dirname));
或者,如果你愿意,你可以设置特定的文件夹:
app.use("/public", express.static(__dirname + "/public"));
app.use("/public2", express.static(__dirname + "/public2"));
为了方便我的静态文件,我有多个目录。 我的一些静态文件在客户端目录中,一些与仪表板相关的文件在 src 目录中,所以现在我的目录结构如下
/
|
client //static files and other stuff
server //server side express, app, controller, models etc
src //other static files
我有两个 angular 应用程序,一个在客户端文件夹中,另一个在 src 文件夹中,我的服务器端路由如下 -
app.route('/app/dashboard-v1').get(function(req,res){
res.sendFile(path.join(__dirname, '../src', 'index.html'));
});
// All undefined asset or api routes should return a 404
app.route('/:url(api|img|src|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
// All other routes should redirect to the index.html
app.route('/*')
.get(function (req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
所以我的第一个 angular 应用程序在 app/dashboard-v1 中,所有其他 urls 都被重定向到 app2
我正在正确获取我的应用程序 2 中的所有文件,但我在我的第二个应用程序中获取所有其他文件的 404。 现在如果我注释掉
// app.route('/:url(api|img|src|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
我从第一个应用程序的所有文件中的第二个应用程序获取 index.html 而不是 404 错误
我的快递配置如下-
if ('production' === env) {
app.use(favicon(path.join(config.root, 'public', 'favicon.ico')));
app.use(express.static(path.join(config.root, 'public')));
app.set('appPath', path.join(config.root, 'public'));
app.use(morgan('dev'));
} else {
app.use(require('connect-livereload')());
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'client')));
app.use(express.static(path.join(config.root, 'src')));
app.set('appPath', path.join(config.root, 'client'));
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
我假设我的路线有问题。我该如何解决 ? 在第一个应用程序 (app/dashboard-v1) 的 index.html 中,我添加了
<base href="/src/" />
我的 index.html 中的所有链接都是相对的,如下是 src/index.html 中的一个块(app/dashboard-v1 url 应用程序)-
<script src="vendor/angular/angular-animate/angular-animate.js"></script>
<script src="vendor/angular/angular-cookies/angular-cookies.js"></script>
当我在浏览器中打开网络控制台时,向服务器发出的请求是这样的 -
Request URL:http://localhost:9000/src/vendor/angular/angular-animate/angular-animate.js
我在浏览器控制台中收到 404 状态码
你 运行 你的例子的 / direcotry 上的应用程序吗?
我认为您必须将 __dirname 设置为静态文件夹
app.use('/',express.static(__dirname));
或者,如果你愿意,你可以设置特定的文件夹:
app.use("/public", express.static(__dirname + "/public"));
app.use("/public2", express.static(__dirname + "/public2"));