2013-05-25 73 views
0

我是express中的新手,我也使用了Backbone Boilerplate。在开发的情况下,当要求/assets/css/index.css我想交付/public/dist/debug/index.css
我做了这一点:不同文件的快速路由

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; 
} 

但由于某些原因我的网页不断得到不正确的文件:/assets/css/index.css

出了什么问题?

回答

0

它应该工作,除非你使用express.static()(我假设正在处理的/assets/css/index.css的要求;如果没有,则更换 :) 之前路线(这将意味着“这就是处理这些请求的路径”静态中间件将首先处理请求)。

此外,而不是你的switch语句中,可以使用app.configure

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(...)); 
+0

我:app.use(express.static(config.root + '/公共')); 这是否意味着我应该使用:res.sendfile('dist/debug/index.css'); (没有“公共”)? – Naor

+0

@Naor它取决于你的目录结构。我认为'res.sendfile'会找到相对于'当前工作目录'(通常是开始运行应用程序的目录)的文件,除非[你提供了'root'选项](http://expressjs.com /api.html#res.sendfile)。但是,既然你得到了一个响应,但不是正确的,这意味着你的自定义'app.get'根本没有被调用。我的答案应该修复。如果不是,则需要提供更多的上下文(如应用程序设置)。 – robertklep