我想显示不在我的网站根目录的公共(public)文件夹中的图像。 我的等级制度:
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
我不知道,我可以将其移动到公共(public)文件夹中并通过 robots.txt 对其进行规则,但我想,也许有更好的解决方案。
最佳答案
您可以将 data
目录作为静态目录,就像您的公共(public)目录 -- 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 - Node : display an image not in public folder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49497594/