Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Express.js (Node.js): How does static file serving really work?

Express.js (Node.js): How does static file serving really work?

Problem

I thought Serving Static files (html, mov, css, jpg, etc.) with Express was going to require some hacks in nginx.

But it seems that Static Files "just work" with Express. No special thing is required.

  • How does Express static file serving really work?

  • Why do some people advocate using nginx for static files? There must be a good reason for this. (Example: https://gist.github.com/joemccann/644282)

  • Also, while a static file is downloading, does the Node.js event queue get blocked? I suppose not, but why not?

Problem courtesy of: lwood

Solution

If the url does not belong to any paths that you registered via app.get(/... (or POST etc.) methods, Express sends the file if the static path is set and the path that you give belongs to some files path under static files directory. File sending is nothing more than streams, they do not block the event queue completely, they are just pushed to the queue and as chunks recieved, they are piped to the HTTP connection, so they happen on the background of your code. The reason of suggesting nginx is that it is more fit to that job, serving files and routing, and it is also written in C, and optimized for that job, where the NodeJS is a more general-purpose tool compared to Nginx.

Solution courtesy of: Mustafa

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Express.js (Node.js): How does static file serving really work?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×