我想显示不在我的webroot公用文件夹中的图像。
我的等级制度:

Webroot
--core
----views
----public <- Here is where stylesheets and other images are
------index.ejs <- Here I want to display the file.jpg
--data
----userdata
------username <- this folder is named by the user
--------assignment <- this folder is named by the assignment
----------file.jpg


我不知道,我可以将其移到公用文件夹中,然后由robots.txt进行控制,但是我认为,也许有更好的解决方案。

最佳答案

您可以将data目录用作静态目录,例如公用目录-Serving static files in Express。您可能需要在静态路由之前设置一些身份验证中间件,否则每个人都可以看到彼此的数据。

这是一个可能看起来像的例子:

// User authentication middleware
app.use(function(req, res, next) {
    // Some implementation that determines the user that made the request.
    req.username = 'foo';
    next();
});

// Serve the public assets to all authenticated users
app.use(express.static('public'));

// Prevent users from accessing other users data
app.use('/data/userdata/{username}/*', function(req, res, next) {
    if (req.username === req.path.username) {
        next();
    } else {
        res.sendStatus(401); // Unauthorized
    }
});

// Serve the data assets to users that passed through the previous route
app.use('/data', express.static('data'));

关于javascript - nodejs:显示不在公共(public)文件夹中的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49497594/

10-10 00:21