先于其他路线投放资产 [英] Serve assets before other routes

查看:81
本文介绍了先于其他路线投放资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js,Express和AngularJS开发一个Web应用程序.

I am developing a web app using Node.js, Express and AngularJS.

我正在从public文件夹提供前端JavaScript,例如HTTP GET /lib/angular/angular.min.js可能会返回AngularJS JavaScript.

I am serving my front-end JavaScript from the public folder, so e.g. that HTTP GET /lib/angular/angular.min.js would presumably return the AngularJS JavaScript.

但是,由于我希望所有请求都由浏览器中的Angular路由器处理,因此我定义了如下的全部路由:

However, as I want all requests to get handled by the Angular router in the browser, I have a catch-all route defined as follows:

app.get('/*', function(req, res) { res.send('template.jade'); });

问题是,此路由将覆盖静态资产路由,在这种情况下,即使请求了静态资产,该路由也会始终运行.

The problem is that this route overrides the static asset routing, in which case it always run, even when a static asset is requested.

是否有一种方法可以告诉Express在自定义路由传播之前处理静态资产?也许还有其他聪明的方法可以避免此问题?

Is there a way to tell Express to process static assets before the custom routes propagate? Are there perhaps any other clever ways of avoiding this issue?

Express配置如下:

The Express configuration is as follows:

// Generated by CoffeeScript 1.7.1
(function() {
  var ExpressConfig, crypto, express, path, pkg;
  crypto = require('crypto');
  express = require('express');
  path = require('path');
  pkg = require('../package');

  ExpressConfig = (function() {
    function ExpressConfig() {}

    ExpressConfig.prototype.configure = function(ENV) {
      var APP_ROOT, app;
      APP_ROOT = path.join(__dirname, '../');
      app = express();
      app.set('port', pkg.config.port);
      app.set('views', APP_ROOT + 'webapp');
      app.set('view engine', 'jade');
      app.use(express.favicon());
      app.use(express.logger('dev'));
      app.use(express.json());
      app.use(express.urlencoded());
      app.use(express.methodOverride());
      app.use(express.cookieParser(crypto.randomBytes(20).toString('hex')));
      app.use(express.session());
      app.use(app.router);
      app.use(require('stylus').middleware(APP_ROOT + 'public'));
      app.use(express["static"](APP_ROOT + 'public'));
      if (ENV === 'development') {
        app.use(express.errorHandler());
      }
      return app;
    };
    return ExpressConfig;
  })();
  module.exports = ExpressConfig;
}).call(this);

//# sourceMappingURL=express-config.map

  • 我可以通过在所有位置登录以检查顺序来检查配置,从而验证配置是否在全部路由定义之前运行.

    • I can verify that the configuration is run before the catch-all route definition, as I have checked it by logging in each place to check the order.

      当删除全部路由时,我还可以验证资产配置是否正常.

      I can also verify that the assets configuration works when the catch-all route is removed.

      推荐答案

      静态中间件应出现在app.router和特定路由之前.

      The static middleware should appear before app.router and the specific route.

      // first
      app.use(express["static"](APP_ROOT + 'public'));
      
      // second
      app.use(app.router);
      
      // last
      app.get('/*',whatever);
      

      这篇关于先于其他路线投放资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆