简体   繁体   English

Express 应用程序提供不需要的静态文件

[英]Express App Serving Unwanted Static Files

I have a react app that is served by express.我有一个由 express 提供的 react 应用程序。 The entire app is included in public/index.html.整个应用程序包含在 public/index.html 中。

Right now my server.js looks like this:现在我的 server.js 看起来像这样:

  1 const express = require('express');
  2 const path = require('path');
  3 const port = process.env.PORT || 8080;
  4 const app = express();
  5
  6 // the __dirname is the current directory from where the script is running
  7 app.use(express.static(__dirname));
  8
  9 app.get('*', (req, res) => {
 10   res.sendFile(path.resolve(__dirname, 'public/index.html'));
 11 });
 12
 13 app.listen(port);

However, somehow, files like package.json and /.ssh/known_hosts are being served, things that obviously shouldn't be avaialible.但是,不知何故,正在提供诸如 package.json 和 /.ssh/known_hosts 之类的文件,这些显然不应该可用。

I'm not sure why app.get('*', (req, res)... isn't catching all requests, and why app.use(express.static(__dirname)); seems to be the only configuration that allows my app to server ANY static files.我不确定为什么app.get('*', (req, res)...没有捕获所有请求,以及为什么app.use(express.static(__dirname));似乎是唯一的配置允许我的应用程序为任何静态文件提供服务。

I haven't had any luck with app.use(express.static(__dirname+'/public'));我对app.use(express.static(__dirname+'/public'));没有任何运气app.use(express.static(__dirname+'/public')); or或者

app.use( '/', express.static(__dirname + '/public'));

or anything else I can find.或者我能找到的任何其他东西。

EDIT---编辑 - -

My project directory looks like:我的项目目录如下所示:

/myproject
    package.json
    server.js
    /public
        index.html

I want ALL requests to simply serve index.html我希望所有请求都只提供 index.html

What I'm still not understanding is why我仍然不明白的是为什么

app.use('*', express.static(path.resolve(__dirname,'/public/index.html')));

does not serve anything.不提供任何服务。

Also, why in the above example, res.sendFile() does nothing without first having called express.static().另外,为什么在上面的例子中, res.sendFile() 在没有首先调用 express.static() 的情况下什么都不做。 If I delete line 7, then nothing is served.如果我删除第 7 行,则不会提供任何服务。

So, never ever do this:所以,永远不要这样做:

app.use(express.static(__dirname));

in your main server directory.在您的主服务器目录中。 That exposes ALL your server files to be viewed.这会公开您要查看的所有服务器文件。

When using express.static() to serve a whole directory, you should create a directory that contains ONLY files intended for public consumption and then point express.static() at that.当使用express.static()为整个目录提供服务时,您应该创建一个仅包含供公共使用的文件的目录,然后将express.static()指向该目录。

I'm not sure why app.get('*', (req, res)... isn't catching all requests我不确定为什么 app.get('*', (req, res)... 没有捕获所有请求

Because that app.get('*', ...) is AFTER your express.static() so if the express.static() finds a matching file, the app.get('*', ...) never even sees the request.因为那个app.get('*', ...)在你的express.static()所以如果express.static()找到匹配的文件, app.get('*', ...)甚至永远不会看到请求。 It's already handled and routing doesn't continue any more.它已经被处理并且路由不再继续。


As always with express.static() to advise on exactly what you should do, we need to know the precise configuration you have.express.static()一样,我们需要知道您的确切配置。 Where are the public files in your file system relative to your main server directory and what URLs are you intending to use in the client to refer to those publicly available files.您的文件系统中相对于您的主服务器目录的公共文件在哪里,以及您打算在客户端中使用哪些 URL 来引用这些公共可用文件。


Here's a theoretical example (since you haven't provided your specifics):这是一个理论示例(因为您尚未提供详细信息):

Let's suppose you have files like this:假设您有这样的文件:

/myproject
   app.js
   /public
       main.css

And, suppose you want to be able to use /main.css as the URL from the client.并且,假设您希望能够使用/main.css作为来自客户端的 URL。 For that, you would just do this from within app.js:为此,您只需在 app.js 中执行此操作:

 app.use(express.static(path.join(__dirname, "public")));

In this first example, where you're serving these static files at the top level path, then you have to make sure there are no naming conflicts between these top level resources and any actual routes you want to serve.在第一个示例中,您在顶级路径中提供这些静态文件,然后您必须确保这些顶级资源与您想要提供的任何实际路由之间没有命名冲突。

If you wanted the client-side URLs to be /assets/main.css , then you would do this:如果您希望客户端 URL 为/assets/main.css ,那么您可以这样做:

 app.use("/assets", express.static(path.join(__dirname, "public")));

In this example, you must make sure that the /public sub-directory (and any sub-directories it might have) contains only files intended to be publicly accessible.在此示例中,您必须确保/public子目录(以及它可能具有的任何子目录)仅包含可公开访问的文件。

Adding a path to the public URL such as /assets removes the chance of a naming conflict between your static assets and your top level routes.添加公共 URL 的路径,例如/assets可以消除静态资产和顶级路由之间命名冲突的可能性。 This can be a good thing because in any multi-person project, it's not uncommon that the person working on the static assets (like CSS files) is different than the person working on server routes so the two main not be directly aware of what names each other is using.这可能是一件好事,因为在任何多人项目中,处理静态资产(如 CSS 文件)的人员与处理服务器路由的人员不同的情况并不少见,因此这两个主要人员不直接知道名称是什么彼此正在使用。 In a single person project, you would certainly just keep it all in your head and avoid accidental naming conflicts that way.在一个人的项目中,你肯定会把这一切都记在脑子里,避免意外的命名冲突。


FYI, my preference for folder organization is more like this:仅供参考,我对文件夹组织的偏好更像是这样:

/myproject
   /server
       app.js
   /public
       main.css

Where it's 100% obvious which are server files and which are public files.哪些是服务器文件哪些是公共文件是 100% 明显的。 Then, to serve the public files from within app.js with a URL of /assets/main.css , I'd do this:然后,为了使用 URL 为/assets/main.css从 app.js 中提供公共文件,我会这样做:

app.use("/assets", express.static(path.join(__dirname, "../public")));

From your comments:从你的评论:

I just want to serve public/index.html for all ('*') GET requests.我只想为所有 ('*') GET 请求提供 public/index.html。 All other data comes from separate apis.所有其他数据来自单独的 api。 it seems that res.sendFile() doesn't work without first using express.static.似乎 res.sendFile() 在没有首先使用 express.static 的情况下不起作用。 The above code resides in server.js上面的代码位于 server.js

If all you want to do is to serve public/index.html for all requests (where public is a sub-directory below where app.js is located), then remove the express.static() middleware entirely.如果您只想为所有请求提供public/index.html (其中publicapp.js所在的子目录),则完全删除express.static()中间件。

Just use this from app.js:只需从 app.js 使用它:

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'public/index.html'));
 });

remove app.use(express.static(__dirname));删除app.use(express.static(__dirname));

as that tells express to load all files that exist因为这告诉 express 加载所有存在的文件

app.use(express.static(__dirname)); will make your app insecure.将使您的应用程序不安全。 Always print out the URLs in the console like path.resolve(__dirname, 'public/index.html') to find out if there is something wrong in it.总是像path.resolve(__dirname, 'public/index.html')这样在控制台中打印出 URL 以找出其中是否有问题。 Seems aiming to the root directory for static files makes problem.似乎针对静态文件的根目录产生问题。 Put them in the conventional public directory then try it out again将它们放在传统的公共目录中,然后再试一次

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

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