56828

What exactly does 'serving static files' mean?

Question:

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.

Answer1:

<blockquote>

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?

</blockquote>

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.

<blockquote>

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

</blockquote>

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.

Answer2:

<blockquote>

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

</blockquote>

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 <a href="http://emberjs.com/" rel="nofollow">Ember.js</a> or <a href="https://angularjs.org/" rel="nofollow">Angular</a> 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.

Recommend

  • Toast not appearing on Android 4.4 KitKat
  • Is there a filesystem plugin available for django?
  • Javascript replace “variables” in HTML code faster
  • Safe regexs to clean serialized DOM?
  • how to auto center objects in a form in access 2007?
  • Submitting form to couchDB through update handler not working
  • C++ stl pop doesn't return [closed]
  • Concise regex extract function in XSLT 2.0
  • How can I stop my python script when another python script is running?
  • Making a URL W3C valid AND work in Ajax Request
  • How to remove last utf8 char of a python string
  • Unable to set a breakpoint on main while debugging a program compiled with Rust 1.10 with GDB
  • How do i find all references to a user control
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • Consuming a web service with the Netbeans Platform
  • Best win32 compiled scripting language? [closed]
  • Reading XML into Datatable gives incorrect DateTime when the time has Time Zone info
  • Does the MySQL IN clause execute the subquery multiple times?
  • Entity Framework ObjectContext: Concurrency
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • 'doc_del_count' bigger than 'doc_count' on CouchDB
  • c# winform DrawToBitmap offscreen
  • Can I update/select from a table in one query?
  • WordPress > setting permalink option via script buggy?
  • How to add closing tag for canvas in three js rendered Canvas?
  • Get the pasted content on document on paste event
  • can variables be set randomly when declaring them again?
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Access user's phone number on iOS 7
  • jQuery: add elements until a particular height is reached
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • JqueryMobile Popup menu is not working
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • When should I choose bucket sort over other sorting algorithms?
  • using HTMLImports.whenReady not working in chrome
  • How to get NHibernate ISession to cache entity not retrieved by primary key