简体   繁体   English

Express.js 试图用变量设置 express.static

[英]Express.js trying to set express.static with variables

I have template settings that vary depending on the subdomain I'm using.我的模板设置因我使用的子域而异。 Therefore I'm trying to find a way to set the express.static dynamically based on the subdomain name I'm using.因此,我试图找到一种根据我使用的子域名动态设置 express.static 的方法。

When app.use runs, template is undefined.当 app.use 运行时,模板未定义。 If I run app.use inside app.get it is out of scope.如果我在 app.get 中运行 app.use,它超出了范围。 And if I try to run app.use from a function it is also out of scope.如果我尝试从一个函数运行 app.use 它也超出了范围。

"template" is a variable that I get in app.get it is my subdomain and http request “模板”是我在 app.get 中获得的一个变量,它是我的子域和 http 请求

app.use('/subdomain/:domain/bower',express.static(path.join(__dirname, '/public/' + **template** + '/bower')));

app.get('/subdomain/:domain',function(req,res,next) {    

        get('/stores/template/' + req.params.domain)
        .then(function(body){
          console.log("template: " + body.toString());
            template = body;                
            res.render('store',{store:req.params.domain});  
        });

});

I'm pretty sure it has to do with scopes, but so far I haven't been able to solve it.我很确定它与范围有关,但到目前为止我还没有解决它。 Any help would be appreciated任何帮助,将不胜感激

Your first app.use() and the express.static() call in it runs when your sever is first starting up.您的第一个app.use()和其中的express.static()调用在您的服务器首次启动时运行。 At that point, the template variable does not yet have a value.此时, template变量还没有值。 You can't really do things the way you're trying to do it.你不能真正按照你想要的方式做事。

app.get() runs immediately also, but its callback is not called until sometime in the future when an http request matching that route actually arrives. app.get()也会立即运行,但直到将来某个时间匹配该路由的 http 请求实际到达时才会调用其回调。 By then, when the template variable gets assigned, it's way too late for it to be useful in your prior app.use() statement.到那时,当template变量被分配时,它在您之前的app.use()语句中变得有用为时已晚。

This would be much easier if your server could just know which sub-domain it was serving when it was originally set up from a configuration file or something like that.如果您的服务器在最初从配置文件或类似文件中设置时可以知道它正在服务哪个子域,那么这将容易得多。 If you intend for the same server to serve many sub-domains at once and you want it to serve different files based on the subdomain, then you will have to code completely differently because you can't just use plain route matching like express.static() does since what you really want is sub-domain + route matching which isn't a built-in feature that I'm aware of.如果您打算让同一台服务器同时为多个子域提供服务,并且希望它基于子域提供不同的文件,那么您将不得不以完全不同的方式进行编码,因为您不能只使用像express.static()这样的普通路由匹配express.static()是因为您真正想要的是子域 + 路由匹配,这不是我所知道的内置功能。

I think if I was trying to solve this, I'd have my first middleware examine the sub-domain of the request and insert it into the front of the URL making a unique pseudo-URL for each sub-domain.我想如果我试图解决这个问题,我会让我的第一个中间件检查请求的子域并将其插入到 URL 的前面,为每个子域创建一个唯一的伪 URL。 Then, you could do normal routing on that pseudo-URL which is what the rest of the middleware and routes will see as the request URL.然后,您可以在该伪 URL 上进行正常路由,这就是中间件和路由的其余部分将视为请求 URL 的内容。

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

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