3

I am considering using nodejs to make a non-realtime app. For example, a website like a blog, forum, or image boards.

I have read that nodejs is good when used for asynchronous jobs. So I am wondering what the result would be when used to serve a lot of static files, like big images, css & js files, etc.

Is it true that when sending a file (suppose it's 2-3MB), the whole server will be blocked until the transfer is complete? I have also read that it might be possible to use the OS's sendfile() syscall to do this job. In this case, does Express support this?

john smith
  • 541
  • 6
  • 24
  • Yes it `Express` does support `sendfile()` function and I have written my blog using `Node.js` and it is great! [Spectrum](https://github.com/alicoding/spectrum). – Ali Aug 09 '13 at 18:47
  • For serving static files on the web, you might want to consider a dedicated CDN (e.g. Amazon CloudFront). – Paul D. Waite Aug 09 '13 at 18:54
  • thank you @Spectrum. Can you post a link to your article? – john smith Aug 09 '13 at 20:50
  • The combination of nginx and node.js is pretty easy to set up and shields node from the onslaught of junk requests that will hit any web server. Nginx can be set to forward only certain url paths to node, and serve the rest from file directories. This setup is useful for allowing node to respond to ajax requests and yet still satisfy the browser same-origin-policy restriction when the javascript is served by nginx. – Paul Aug 09 '13 at 20:59
  • Related http://stackoverflow.com/questions/11311672/building-a-website-using-node-js-best-practice – Benjamin Gruenbaum Aug 12 '13 at 21:26

1 Answers1

6

No it is not true. You can easily send files that are large (much larger than 2-3 MB) without blocking. People who complain about things like this blocking the Node event loop just don't know what they're doing.

You don't necessarily need to use Express to get this behavior.

That being said, if what you want is a file server, there's no reason to use NodeJS. Just point Apache at a directory, and let it fly. Why reinvent the wheel just to use the new sexy technology, when old faithful does just fine?

If you would like to use node as a simple http server, may I recommend the very simple command line module.

https://npmjs.org/package/http-server

I haven't looked at the code of the module, but it is likely not optimized for large files. Let's define large in this case, as files that are not easily cached in memory(whatever this means for your setup). If your use case calls for more optimization (piping "large" files for example) you may still have to write your own module, but this will get you started very quickly, and is an excellent utility to use for general development when you need to serve up a directory real quick.

MobA11y
  • 18,425
  • 3
  • 49
  • 76
  • 1
    Also good examples, just through out the first one that came to my mind to be honest. – MobA11y Aug 09 '13 at 19:15
  • Thanks @ChrisCM. That's really good to know. I was thinking to use node especially for its module socket.io which might turn useful when (hopefully) my website will turn bigger, so I will not have to add complexity to the system. – john smith Aug 09 '13 at 20:53