简体   繁体   English

Express JS获取目录中所有静态文件的404

[英]Express js getting 404 for all static files in a directory

I have multiple directories for my convenience for my static files. 为了方便我的静态文件,我有多个目录。 Some of my static files are in client directory and some dashboard related files are in src directory so now my directory structure is as follows 我的某些静态文件位于客户端目录中,而某些与仪表板相关的文件位于src目录中,因此现在我的目录结构如下

/
|
client //static files and other stuff
server //server side express, app, controller, models etc
src //other static files

I have two angular apps one in client folder and another in src folder and my server side routes are as follows - 我有两个有角度的应用程序,一个在客户端文件夹中,另一个在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'));
        });

So my first angular app is in app/dashboard-v1 and all other urls are redirected to app2 所以我的第一个有角度的应用程序在app / dashboard-v1中,所有其他网址都重定向到app2

I am getting all the files in my app2 correctly but I am getting 404 for all other files in my second app. 我正确地获取了我的app2中的所有文件,但是我的第二个应用程序中的所有其他文件却得到了404。 now If I comment out the 现在如果我注释掉

//  app.route('/:url(api|img|src|auth|components|app|bower_components|assets)/*')
            .get(errors[404]);

I am getting my index.html from second app in all files in first app instead of the 404 error 我从第一个应用程序的所有文件中的第二个应用程序获取了index.html,而不是404错误

My express configuration is as follows - 我的快速配置如下-

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
  }

I am assuming something is wrong with my routes. 我认为自己的路线有问题。 How do I fix this ? 我该如何解决 ? In my index.html in first app(app/dashboard-v1) I have added the 在第一个应用程序(app / dashboard-v1)的index.html中,我添加了

<base href="/src/" />

and all the links inside my index.html are relative like the following is a block from src/index.html (app/dashboard-v1 url app)- 并且我的index.html中的所有链接都是相对的,如下所示是src / index.html(应用程序/ dashboard-v1网址应用程序)的一个代码块-

<script src="vendor/angular/angular-animate/angular-animate.js"></script>
  <script src="vendor/angular/angular-cookies/angular-cookies.js"></script>

and when I open the network console in my browser the request that is made to the server is like this - 当我在浏览器中打开网络控制台时,对服务器的请求是这样的-

Request URL:http://localhost:9000/src/vendor/angular/angular-animate/angular-animate.js

to which I am getting a 404 status code in browser console 我在浏览器控制台中收到404状态代码的信息

Do you run the app on the / direcotry of your example? 您是否在示例的/目录中运行该应用程序?

I think that you ahve to set __dirname as the static folder 我认为您可以将__dirname设置为静态文件夹

app.use('/',express.static(__dirname));

or if you want you can set specific folders likt this: 或者,如果您愿意,可以设置特定的文件夹:

app.use("/public", express.static(__dirname + "/public"));
app.use("/public2", express.static(__dirname + "/public2"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM