简体   繁体   English

Node.js、Express 和 Mongoose 提供多个静态文件

[英]Node.js, Express and Mongoose serving several static files

With Node.js, Express and Mongoose, I am serving several static files synchronously within multiple sub-directories with the following code (which works fine):使用 Node.js、Express 和 Mongoose,我使用以下代码在多个子目录中同步提供多个静态文件(工作正常):

fs.readdirSync(__dirname + "/../../../np-site/themes/default/client/")
    .forEach(function (dir) {
        app.use(express.static(__dirname +
            "/../../../np-site/themes/default/client/" + dir)
        );
    });

However, part of the url must be dynamic depending on a database entry value:但是,部分 url 必须是动态的,具体取决于数据库条目值:

express.static(__dirname + "/../../../np-site/themes/" + theme + "/client/" + dir)

I have tried several different ways, to no avail.我尝试了几种不同的方法,都无济于事。 The following is the first attempt I made, which made the most sense to me at the time (the App model is a single object that can only be updated and retrieved):以下是我的第一次尝试,当时对我来说最有意义(App模型是一个只能更新和检索的对象):

App.find(function (err, appSettings) {
    fs.readdirSync(__dirname + "/../../../np-site/themes/" + appSettings[0].theme +
        "/client/").forEach(function (dir) {
            app.use(express.static(__dirname + "/../../../np-site/themes/" +
            appSettings[0].theme + "/client/" + dir)
        );
    });
});

This however does not work, and no static files are served as a result.然而,这不起作用,因此不会提供静态文件。

The object that I need to access is available (discovered via console.log), but something is not right.我需要访问的对象可用(通过 console.log 发现),但有些地方不对。

The browser console does return an infrequent error message stating the MIME type of the files (CSS) is not correct, but I believe this is because the app cannot find correct directories (removing the method altogether has the same result).浏览器控制台确实会返回一条罕见的错误消息,指出文件的 MIME 类型 (CSS) 不正确,但我相信这是因为应用程序找不到正确的目录(完全删除该方法具有相同的结果)。

I'm thinking that it has something to do with app.use within a Mongoose method, but I am not certain.我认为它与 Mongoose 方法中的 app.use 有关,但我不确定。

Could someone please shed some light towards this frustrated soul?有人可以对这个沮丧的灵魂有所了解吗? I feel as though I am on the wrong path.我感觉好像我走错了路。

The problem is that you're adding your middleware asynchronously (because App.find() is most likely performing an asynchronous action), which causes your dynamic middleware to get added (probably) after all of your statically defined middleware and route handlers.问题是您正在异步添加中间件(因为App.find()最有可能执行异步操作),这会导致您的动态中间件被添加(可能)所有静态定义的中间件和路由处理程序之后。

Express executes middleware and route handlers in the order that they are added. Express 按照添加的顺序执行中间件和路由处理程序。

The solution would be to delay adding any other middleware or route handlers until after your fs.readdirSync() (but inside your App.find() callback).该解决方案将延迟添加任何其它的中间件或路由处理,直到你的fs.readdirSync()但你的内部App.find()回调)。 A simple way to do this is to simply put your other middleware and route handler additions inside a function that you call after your fs.readdirSync() :一种简单的方法是简单地将其他中间件和路由处理程序添加到您在fs.readdirSync()之后调用的函数中:

var baseThemePath = __dirname + "/../../../np-site/themes/";
App.find(function (err, appSettings) {
  var themePath = baseThemePath + appSettings[0].theme + "/client/";
  fs.readdirSync(themePath).forEach(function(dir) {
    app.use(express.static(themePath + dir));
  });
  setupStatic();
});

function setupStatic() {
  app.use(express.static('public'));
  app.get('/', function(req, res) {
    res.send('Hello world');
  });
  // ...
}

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

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