Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does 'serving static files' mean?

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.

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.

I understand javascript files that are included in html or css are best stored in the client directory?

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?

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?

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.

like image 875
Robin-Hoodie Avatar asked Mar 07 '15 19:03

Robin-Hoodie


People also ask

What is serving a static file?

Static files are typically files such as scripts, CSS files, images, etc... that aren't server-generated, but must be sent to the browser when requested. If node. js is your web server, it does not serve any static files by default, you must configure it to serve the static content you want it to serve. You use node.

What does it mean to serve files?

At its core, HTTP is a file transfer protocol. the path element of a URL is based on a Unix file path. most web servers directly map incoming URL paths to filesystem paths. because these files are stored on disk on the server, they are called static files.

Where do I serve static files?

To serve static files for Go 1.12+ in the standard environment, you define the handlers in your app. yaml file using either the static_dir or static_files elements. The content in the static files or static directories are unaffected by the scaling settings in your app.

What is a static server?

A static web server, or stack, consists of a computer (hardware) with an HTTP server (software). We call it "static" because the server sends its hosted files as-is to your browser. A dynamic web server consists of a static web server plus extra software, most commonly an application server and a database.


2 Answers

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?

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?

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.

You should keep your application code (code that serves dynamic responses, such as an API server) within your Node.js application.

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.

like image 143
Brad Avatar answered Oct 07 '22 14:10

Brad


Sometimes I see people use express.static for static files, what exactly is implied by static files here?

I believe you're referring to this bit of code usually found an an express app's app.js file:

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:

- 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/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.

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.

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.

like image 39
JDillon522 Avatar answered Oct 07 '22 16:10

JDillon522