2015-10-24 154 views
0

为了方便我的静态文件,我有多个目录。 我的一些静态文件是在客户端目录和一些仪表板相关的文件都在src目录所以现在我的目录结构如下快递js获得404目录中的所有静态文件

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

我有两个角应用一个客户端文件夹中,另一个在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')); 
     }); 

所以我的第一个角的应用程序是在app /仪表板-V1和其他所有网址被重定向到APP 2

我正确,但是我让我的APP 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 
    } 

我假设我的路线有问题。我该如何解决 ? 以我在第一个应用程序(应用程序/仪表板-V1)的index.html我已经添加了

<base href="/src/" /> 

和我的index.html内的所有链接都相对类似下面是从SRC/index.html中的块(APP /仪表板-V1网址的应用程序) -

<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状态代码

回答

1

您是否在您的示例的/ direcotry上运行应用程序?

我认为你ahve设置__dirname为静态文件夹

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

,或者如果你愿意,你可以设置特定的文件夹likt这样的:

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