防止通过其他用途/快递处理丢失的文件 [英] prevent handling of missing files by other use/get in express

查看:32
本文介绍了防止通过其他用途/快递处理丢失的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留文件夹/public 的内容,以供本地应用程序使用本地文件.但是,当文件丢失时,用户将获得 index.html 页面,而不是获取404.

I'd like to keep the contents of the folder /public for use of local files by a local app. However, when a file is missing, instead of getting the 404, the user is getting the index.html page.

我想做的是让用户访问/public/* 下不存在的任何资源时获得404,但是让react应用处理来自index.html的所有其他内容

What I would like to do is have user get the 404 when accessing any resource that does not exist for anything under /public/*, but have the react app handle everything else from index.html

这是我的设置:

app.use('/public', express.static(path.resolve(myPath, 'public')));
app.get('*', (req, res) => {
  res.sendFile(path.resolve(myPath, 'index.html'));
}

get应该包含正则表达式而不包含 public 匹配项,还是有办法通过使用来处理呢?

should the get include a regex to not include public matches, or is there a way to handle this with use?

推荐答案

问题是您已明确要求Express进入 public 文件夹,以查找用户使用此行请求的任何文件:

The problem is that you've explicitly told express to look into your public folder for ANY file that the user requests with this line:

app.use('/public', express.static(path.resolve(myPath, 'public')));

Express默认情况下不提供文件.默认情况下,它不公开任何内容.因此,要做的第一件事是删除使整个目录公开的代码行.而且,然后,如果您想从该文件夹中提供某些特定的内容,则需要只针对您要公开的文件进行非常特定的路由,或者需要将公共"文件移到可以存放所有内容的目录中公开,然后将 express.static()指向该目录.

Express, by default, serves NO files. It makes nothing public by default. So, the first thing to do is to remove the line of code that makes that entire directory public. And, then if you want to serve some specific things from that folder, you need to either make very specific routes to only the files you want to be public or you need to move the "public" files out to a directory where everything can be public and then point express.static() at that directory.

然后,您添加了另一行以响应一个文件可能出现的任何请求:

Then, you've added another line to respond to any request possible with one file:

app.get('*', (req, res) => {
    res.sendFile(path.resolve(myPath, 'index.html'));
}

这根本不是真正应该使用Express的方式.这里是一些思考的步骤:

This is just not really how you should be using express at all. Here are some steps to think through:

  1. 找出希望Express自动提供哪些静态文件(无需为每个文件建立特定路径).
  2. 然后,整理这些文件,使它们位于服务器硬盘驱动器上自己的子目录中.
  3. 然后,您可以将 express.static()指向该目录,而不必担心它会提供您不想自动公开的文件.
  4. 考虑将事物组织到一个层次结构中(不是必需的,但有时更易于管理),以便将 css 文件放在一个地方,将客户端 js 放在另一个地方,等...,例如,在布置层次结构时,此处的/css/css文件和此处的/js/js文件.然后,如果需要,您可以分别控制每种文件的提供,这可以使维护变得更容易(因为不同的人经常维护CSS文件和JS文件).
  5. 然后,为其他类型的文件设计要处理的特定路由.
  6. 然后,添加错误处理程序路由,该路由确定当未找到其他路由处理程序时应返回到浏览器的内容.此处中对此进行了描述.另外,请注意,根据是否将NODE_ENV环境变量设置为生产,Express具有不同的默认错误处理程序.
  7. 请勿使用 * 路由来处理具有相同内容的所有内容,包括您不想为其提供特定页面的内容.您不希望搜索引擎为您没有原始内容的内容建立索引,也不希望用户只是因为您在路由处理程序中使用了 * 而将不希望的URL标记为书签.
  1. Figure out which static files you want Express to serve automatically (without a specific route being made for each file).
  2. Then, organize those files so they are in their own sub-directory on your server hard drive.
  3. Then, you can point express.static() at that directory without any fear of it serving files you don't want to be automatically public.
  4. Think about organizing things into a hierarchy (not required, but sometimes simpler to manage) so that css files might be one place, client-side js another place, etc... such as /css/css files here and /js/js files here when laying out your hierarchy. Then, you can control the serving of each type of file separately if wanted and it may make maintenance easier (since separate people often maintain CSS files and JS files).
  5. Then, design specific routes you want handled for other types of files.
  6. Then, add an error handler route which determines what should be returned to the browser when no other route handler was found. How to do that is described here. Also, note that Express has a different default error handler based on whether the NODE_ENV environment variable is set to production or not.
  7. Don't use * routes that handle everything with the same content, including things you don't want to provide a specific page for. You don't want search engines to index things that you don't have original content for and you don't want users to bookmark unintended URLs just because you happen to be using * in a route handler.

这篇关于防止通过其他用途/快递处理丢失的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆