简体   繁体   English

表示路由到其他文件

[英]express route to different file

I am new in express and I am using Backbone Boilerplate also. 我是快递新手,也正在使用Backbone Boilerplate。 In case of development, when asking for /assets/css/index.css I want to deliver /public/dist/debug/index.css . 如果需要开发,在请求/assets/css/index.css我想提供/public/dist/debug/index.css
I've made this: 我做了这个:

var env = process.env.NODE_ENV || 'development';
switch (env) {
    case 'development':
        app.get('/assets/css/index.css', function(req, res) {
            res.sendfile('public/dist/debug/index.css');
        });
        break;
}

But for some reason my page keep getting the incorrect file: /assets/css/index.css . 但是由于某种原因,我的页面不断获取错误的文件: /assets/css/index.css

What is wrong? 怎么了?

It should work, unless you use express.static() (which I assume is handling the requests for /assets/css/index.css ; if not, replace with 'the route that's handling those requests' :) before your route (which would mean that the static middleware would get to handle the request first). 它将起作用,除非您在路由 之前 (其中使用了express.static() (我假设正在处理/assets/css/index.css的请求;否则,请替换为“正在处理这些请求的路由 :))。表示静态中间件将首先处理请求)。

Also, instead of your switch statement, you can use app.configure : 另外,可以使用app.configure代替switch语句:

app.configure('development', function() {
  // this code will only run when in development mode
  app.get('/assets/css/index.css', function(req, res) {
    res.sendfile('public/dist/debug/index.css');
  });
});

// static middleware after your route
app.use(express.static(...));

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

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