Javascript “提供静态文件”究竟是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28918845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exactly does 'serving static files' mean?
提问by Robin-Hoodie
So far I've always been developing my clientside applications without any of my own servers running behind it, using Webstorm's built-in webserver to serve my content.
到目前为止,我一直在开发我的客户端应用程序,没有任何我自己的服务器在它后面运行,使用 Webstorm 的内置网络服务器来提供我的内容。
What I frequently see when people use Node with Express to act as their webserver is the debate on if you should put your html files with node or with the client code.
当人们使用 Node 和 Express 作为他们的网络服务器时,我经常看到的是关于是否应该将 html 文件与 node 或客户端代码一起放置的争论。
I understand javascript files that are included in html or css are best stored in the client directory?
我了解 html 或 css 中包含的 javascript 文件最好存储在客户端目录中吗?
So my first question is, with a folder structure like this
所以我的第一个问题是,像这样的文件夹结构
app/
client/ js files
server/ node files
Should you include your html pages in your server or your client directory?
您应该在服务器或客户端目录中包含 html 页面吗?
Secondly:
其次:
Sometimes I see people use express.staticfor static files, what exactly is implied by static files here? Today most websites are not static documents anymore but are files that get altered by javascript by manipulating the DOM, so I don't think any html files should be considered as static files?
有时我看到人们使用express.static静态文件,这里的静态文件到底隐含了什么?今天大多数网站不再是静态文件,而是通过操作 DOM 被 javascript 改变的文件,所以我认为任何 html 文件都不应该被视为静态文件?
As far as I can see, the only advantage I have with using Node instead of the built-in webserver is if I want to have database access.
据我所知,使用 Node 而不是内置网络服务器的唯一优势是我是否想要访问数据库。
回答by Brad
Today most websites are not static documents anymore but are files that get altered by javascript by manipulating the DOM, so I don't think any html files should be considered as static files?
今天大多数网站不再是静态文件,而是通过操作 DOM 被 javascript 改变的文件,所以我认为任何 html 文件都不应该被视为静态文件?
The files for your pages themselves are still static. That is, you are not creating them dynamically with server-side code. What happens in the browser doesn't matter in this context... the idea is that you do not need to generate these files on the fly, as their content does not change.
您的页面文件本身仍然是静态的。也就是说,您不是使用服务器端代码动态创建它们。在这种情况下,浏览器中发生的事情无关紧要......这个想法是您不需要即时生成这些文件,因为它们的内容不会改变。
I understand javascript files that are included in html or css are best stored in the client directory?
我了解 html 或 css 中包含的 javascript 文件最好存储在客户端目录中吗?
Where you store your files on the server doesn't matter. What does matter is that you don't generally want to serve static files from your Node.js application. Tools like express.staticare for convenience only. Sometimes, you may have a low traffic application. In these cases, it is perfectly acceptable to serve files with your Node.js app. For anything with a decent traffic load, it's best to leave static serving up to a real web server such as Nginx, since these servers are far more efficient than your Node.js application.
您在服务器上存储文件的位置并不重要。重要的是您通常不希望从您的 Node.js 应用程序提供静态文件。像这样express.static的工具只是为了方便。有时,您可能有一个低流量的应用程序。在这些情况下,使用 Node.js 应用程序提供文件是完全可以接受的。对于任何具有良好流量负载的内容,最好将静态服务留给真正的 Web 服务器,例如 Nginx,因为这些服务器比您的 Node.js 应用程序高效得多。
You should keep your application code (code that serves dynamic responses, such as an API server) within your Node.js application.
您应该将您的应用程序代码(提供动态响应的代码,例如 API 服务器)保留在您的 Node.js 应用程序中。
It's also a good idea to put your Node.js application behind a proxy like Nginx so that the proxy can handle all of the client interaction (such as spoon-feeding slow clients) leaving your Node.js application to do what it does best. Again though, in low traffic situations it doesn't matter.
将您的 Node.js 应用程序放在像 Nginx 这样的代理之后也是一个好主意,这样代理就可以处理所有客户端交互(例如勺子馈送慢客户端),让您的 Node.js 应用程序做它最擅长的事情。不过,在低流量情况下,这无关紧要。
回答by JDillon522
Sometimes I see people use express.static for static files, what exactly is implied by static files here?
有时我看到人们将 express.static 用于静态文件,这里的静态文件到底隐含了什么?
I believe you're referring to this bit of code usually found an an express app's app.jsfile:
我相信你指的是这段代码通常发现一个快速应用程序的app.js文件:
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
express.static()is a bit of middleware that maps directory names to the path directory for easy lookup. Usually you'll have:
express.static()是一个中间件,它将目录名称映射到路径目录以便于查找。通常你会有:
- public
|_ javascripts
|_ stylesheets
|_ images
If you have a script in your javascriptsdirectory, you dont have to type out the full path to include it. Just:
如果您的javascripts目录中有脚本,则不必键入完整路径以包含它。只是:
./javascripts/script.js
./javascripts/script.js
Static files are best considered as files that are not include by something like NPM or Bower. They are your own scripts, stylesheets, images, etc. It has nothing to do with the page being dynamic or static.
最好将静态文件视为未包含在 NPM 或 Bower 之类的文件中。它们是您自己的脚本、样式表、图像等。它与页面是动态的还是静态的无关。
As to your first question:
至于你的第一个问题:
Im personally not sure of the need for that kind of project architecture if you're using node. If you're using node and something like Ember.jsor Angularfor your clientside app, than I would personally put my actual application scripts inside of the public/javascripts/directory. But thats just me.
如果您使用 node.js,我个人不确定是否需要这种项目架构。如果您在客户端应用程序中使用 node 和Ember.js或Angular 之类的东西,那么我个人会将我的实际应用程序脚本放在public/javascripts/目录中。但这只是我。
At the end of the day, pick a project structure you like, and stick with it. If other people are working on the project however, stick with common conventions. It makes life easier.
在一天结束时,选择一个你喜欢的项目结构,并坚持下去。但是,如果其他人正在从事该项目,请坚持通用约定。它让生活更轻松。

