“服务静态文件”究竟是什么意思,你应该这样做吗?

时间:2022-01-13 11:59:18

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的内置Web服务器来提供我的内容。

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 with Express充当他们的网络服务器时,我经常看到的是关于你是否应该将html文件与节点或客户端代码放在一起的争论。

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.static for 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而不是内置Web服务器的唯一优势是,如果我想拥有数据库访问权限。

2 个解决方案

#1


24  

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.static are 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.

您应该在Node.js应用程序中保留您的应用程序代码(提供动态响应的代码,例如API服务器)。

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应用程序做到最好。但是,在低流量情况下,这并不重要。

#2


6  

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.js file:

我相信你指的是这段代码,通常会发现一个快递应用程序的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 javascripts directory, 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.js or Angular for your clientside app, than I would personally put my actual application scripts inside of the public/javascripts/ directory. But thats just me.

如果您使用节点,我个人不确定是否需要这种项目架构。如果您正在为您的客户端应用程序使用节点和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.

在一天结束时,选择一个你喜欢的项目结构,并坚持下去。但是,如果其他人正在从事该项目,请坚持使用共同的惯例。它让生活更轻松。

#1


24  

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.static are 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.

您应该在Node.js应用程序中保留您的应用程序代码(提供动态响应的代码,例如API服务器)。

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应用程序做到最好。但是,在低流量情况下,这并不重要。

#2


6  

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.js file:

我相信你指的是这段代码,通常会发现一个快递应用程序的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 javascripts directory, 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.js or Angular for your clientside app, than I would personally put my actual application scripts inside of the public/javascripts/ directory. But thats just me.

如果您使用节点,我个人不确定是否需要这种项目架构。如果您正在为您的客户端应用程序使用节点和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.

在一天结束时,选择一个你喜欢的项目结构,并坚持下去。但是,如果其他人正在从事该项目,请坚持使用共同的惯例。它让生活更轻松。