简体   繁体   English

我可以对目录中的任何文件使用什么快速路由?

[英]What express route can I use for any file in a directory?

I have a directory of PDF files that I only want authorized users to access.我有一个我只希望授权用户访问的PDF文件目录。 Currently, the issue is when I navigate to the URL that contains a given file in the folder the get callback isn't running.目前,问题是当我导航到文件夹中包含给定文件的 URL 时,get 回调未运行。 For example, I expect when I go to localhost:3000/Buyer_templates/file.pdf it will run the callback in the get.例如,我希望当我去localhost:3000/Buyer_templates/file.pdf它会在 get 中运行回调。 Currently, it allows me to view the pdf and the callback is not executed.目前,它允许我查看 pdf 并且不执行回调。

app.get('/Buyer_templates/*', (req,res) => {
    res.redirect('/')
})

The above code works if the url isn't a file (eg /Buyer_templates/blahblahblah )如果 url 不是文件(例如/Buyer_templates/blahblahblah ),则上述代码有效

However, when I put in a file name (eg Buyer_templates/file.pdf ) the callback isn't activated.但是,当我输入文件名(例如Buyer_templates/file.pdf )时,回调未激活。

I also tried these paths in the get我也在 get 中尝试了这些路径

/Buyer_templates/*.*

/Buyer_templates/(.*)

The issue is the callback isn't being executed.问题是没有执行回调。 How can I make it so when I put in any PDF file name within the URL the callback is run?当我在 URL 中输入任何PDF文件名时,如何才能运行回调?

When you configure static route in Express, any files from within the hierarchy from the root folder are served automatically.在 Express 中配置static路由时,将自动提供来自根文件夹层次结构中的任何文件。 For example, if I had a folder structure like so:例如,如果我有这样的文件夹结构:

root
- public
-- css
-- js
-- img

And my server looked like我的服务器看起来像

app.use(express.static('public'));
app.use((req, res, next) => {
  console.log(`REQUEST LOGGED: ${req.url}`);
  return next();
});
...

When I access any file URL that resides within that static root eg当我访问驻留在该静态根目录中的任何文件 URL 时,例如

/css/somefile.css
/js/somefile.js
/img/somefile.jpg

Then you would notice that the request never hits the console.log , that's because Express understands that these are a.然后你会注意到请求从未命中console.log ,那是因为 Express 知道这些是 a。 files and b.文件和 b. reside within the static directory, and thus don't require any further processing (hence why you notice if you try non-file URLs they do process further).驻留在静态目录中,因此不需要任何进一步处理(因此您会注意到,如果您尝试非文件 URL,它们进一步处理)。

In terms of adding some form of authorization to static files, there are a couple of ways you can do it eg在向静态文件添加某种形式的授权方面,有几种方法可以做到,例如

  • exclude this particular folder from the static route and process the authorization that you would do in any other route从静态路由中排除此特定文件夹并处理您将在任何其他路由中执行的授权
app.get('/Buyer_templates/*', (req, res, next) => if (/* some authorization check */) { return res.sendFile('...'); } else { return res.status(403).send('Unauthorized'); } });
  • add your authorization middleware before you setup the static route (although it would mean a bit more work determining which URLs to check etc.)设置静态路由之前添加授权中间件(尽管这意味着需要做更多的工作来确定要检查的 URL 等)
 app.use((req, res, next) => { if (req.path.startsWith('/Buyer_templates')) { return res.status(403).send('Unauthorized'); } return next(); }) app.use(express.static(__dirname));

The examples above are just simplistic for demonstration, but they should give you the desired effect.上面的例子只是简单的演示,但它们应该会给你想要的效果。 Also, worth pointing out that express.static(__dirname) isn't advisable as it would make all files accessible from the root of your app, which would include any code you may have hosted on the server.此外,值得指出的是express.static(__dirname)是不可取的,因为它会使所有文件都可以从您的应用程序的根目录访问,其中包括您可能托管在服务器上的任何代码。 Be more specific ie express.static('Buyer_templates')更具体,即express.static('Buyer_templates')

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

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