简体   繁体   English

正则表达式公共资产路线

[英]Regex routes for public assets

I have "widgets" which include both client & and server .coffee files (think of client as a Backbone.js model/view and server as the correlating ExpressJS routes), all under the root project: 我有“小部件”,其中包括客户端和服务器.coffee文件(将客户端视为Backbone.js模型/视图,将服务器视为相关的ExpressJS路由),所有这些都位于根项目下:

my-node-expressjs3-project/
  src/
    widget1/
      client/
        app.coffee
      server/
        routes.coffee
    widget2/
      client/
        app.coffee
      server/
        routes.coffee
  app/
    widget1/
      client/
        app.js 
      server/
        routes.js
    widget2/
      client/
        app.js 
      server/
        routes.js
  public/

All .coffee files compile to app/ . 所有.coffee文件都编译为app/ I am trying to keep the widgets referentially intact, and just allow the client/ folder to be served for "widgets". 我试图保持小部件的参照完整性,只允许将client/文件夹提供给“小部件”。 Problem 1 - I can't get it to work. 问题1-我无法正常工作。 Problem 2 - if 2 widgets contain the same app.js file, they would appear the same under the way I thought to solve the problem: 问题2-如果2 app.js部件包含相同的app.js文件,则在我想解决该问题的方式下它们将显示为相同:

app.use express['static'](__dirname + '/public')
app.use express['static'](__dirname + '/app/*/client/')

How would I do this? 我该怎么做?

Regarding problem one, node doesn't have a built-in way to search a filesystem like this using globbing. 关于问题一,节点没有内置的方法来使用全局搜索来搜索这样的文件系统。 If you have non-widgety things in your app directory you will need to filter them out manually, or you can use something like the awesome node-findit package. 如果您的应用程序目录中有非电子产品,则需要手动将其过滤掉,或者您可以使用很棒的node-findit软件包。

Regarding problem two, once you have the list of widgets, you can just mount the static middleware at that subUrl for each widget. 关于第二个问题,一旦有了小部件列表,就可以将静态中间件安装在每个小部件的该subUrl上。 The app.use function takes an optional mount path. app.use函数采用可选的安装路径。

var widgetDirs = fs.readdirSync('app');

widgetDirs.forEach(function(widgetDir) {
    app.use(widgetDir, express.static(__dirname + '/app/' + widgetDir + '/client/'));
});

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

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