/node-cdn

A Simple CDN Module for NodeJS. Initially will only upload to Amazon S3. but plan is to support several CDNs.

Primary LanguageJavaScript

#CDN Uploader

What This Module Does

  • Creates a REST API Wrapper Method which allows any app to upload a static file (image/css/js/json) to Amazon WebServices (AWS) Simple Storage Service (S3) and returns a URL for the uploaded file.

Intro

If you have no idea what a Content Delivery Network CDN is, in a nutshell it means the people visiting have to wait less time (for pages to load). This is because the "content" (images, text, videos and "scripts" - such as CSS and JavaScript are stored/served closer to them on world-wide-web)

CDN Diagram shows one server vs several servers distributed near the client

Take some time to watch this Video on How CDNs Work: http://youtu.be/dZ5aXGFnxI4 [Watch in 720p Full Screen]

Using a CDN is the single greatest performance boost you can give your Web Application.

See:

If you are not using a CDN for your NodeJS projects, this module will help you get started. ;-)

The cost of using a CDN has decreased greatly in the past few years due to increased competition. What used to cost thousands of dollars (or pounds/euros/yen...) now costs just pennies. The hardest part is getting everything set up ...

There are several great CDN Providers out there; the main ones are:

  • Akamai (the industry leader)
  • NetDna - almost as fast as Akamai but with transparent pay-as-you-go pricing. But there's a minimum fee of $800 per month!! Not exactly BootStrap sort of Money...!
  • RackSpace Cloud Files - Uses Akamai's technology but offers pay-as-you-go pricing (more expensive than Amazon's Cloudfront)
  • MaxCDN - decent but annoying annual pricing and "overage" charges!
  • Amazon Web Services (AWS) CloudFront Offers 95% of the speed of Akamai for a fraction (half?) the price and no contract or minimum spend!
  • Amazon S3 is not considered to be a "Real CDN" by purists because its not designed to minimise latency (by distributing copies of files to several nodes) It does offer many of the benefits of a CDN (different server for static content speeds up page load) for minimal costs! And most importantly the "Free Tier" means you can use S3 for FREE for the first year (below 5GB - which trust me is plenty of space!)

For a Report on CDN Performance and Availability visit: http://www.cedexis.com/country-reports/ (click on the "CDNs" tab on the main menu) shows clearly that you get more when you pay more. At the time of writing Akamai has an average latency of 102ms ' whereas AWS's CloudFront is 106ms; a negligeable difference.

For our purposes using Amazon's S3 + CloudFront will provide a massive boost to any NodeJS (or any other Web App!) and is a great way to get started!

If you are completely new to Amazon Web Services (AWS) I recommend watching @Sayanee's build-podcast.com http://vimeo.com/59749907


Implementation

We are using:

If you are toally new to CoffeeScript, welcome to the Future!!

There has been much debate in the JavaScript / NodeJS Community as to wether you should learn CoffeeScript or just continue writing (mediocre) JavaScript ... It boils down to Five Things:

  • Code Clarity - CoffeeScript forces indentation and is "whitespace significant" (like Python)
  • Consistent Quality - CoffeeScript is designed to Pass JS Lint meaning it follows all the JavaScript Best Practices described in Douglas Crockford's "JavaScript: The Good Parts".

Essentially people new to JS development hack at the problem or copy-paste the "solution" by googling, by contrast, people that do the Homework and have experience of debugging terrible (rushed/unstructured/untested/undocumented) JS code learn to appreciate the value of enforced standards.

CoffeeScript forces you to respect whitespace. While initially annoying (for those of you who don't have a background in Python), you will soon see the benefit when working in a Team of having everbody's code look consistent.

Here are a couple of simple tutorials to get you started:

Must move this block to CoffeeScript Blog Post / Rant ... ;-)

Testing with Jasmine

For our Unit Testing http://pivotal.github.io/jasmine/ If you are new to using Jasmine I recommend you take the time to learn it well by working through these tutorials:

Templating with ECT

Note: Templating is ONLY used in testing. This module does not dictate any templating engine. You can still use what ever you are using. ;-)

On previous projects I've used Jade, EJS and CoffeeKup but decided to try ECT because it claims to be Faster than the more established alternatives. see: Templating Engine Benchmark https://github.com/baryshev/template-benchmark

Long story short, my 3 reasons for chosing ECT for this project:

  • Its (a LOT) faster than Jade / EJS etc!
  • The Templates Look like HTML (closely matches output so easier to debug)
  • Minimal Learning Curve for people who are familiar with HTML.
  • (Bonus) A change from Jade ... ;-)

Primary Dependency: Knox

The Knox module by LearnBoost: https://github.com/LearnBoost/knox it is the most popular Amazon WebServices (AWS) Simple Storage System (S3) Module for NodeJS - read the examples on the module's GitHub page and then subtract all the superfluous "punctuation" (curly braces, semi-colons and "vars"...)

JSDom (Client Side Testing) -- https://github.com/tmpvar/jsdom

Stuck?

If you get stuck or have any questions tweet me! @nelsonic


Notes to Future-Self:

If you need to update the version of Jasmine go to: https://github.com/pivotal/jasmine/downloads

I'm using CDNJS http://cdnjs.com/ to source all Backbone JS dependencies in SpecRunner.html (but you do not need to know Backbone to use this module!)

If you need to update any of these version numbers, go for it and submit a patch to me!

In production you may want to host these Static JS files on your own CDN to have full control over what code you are running (#Security), but I trust CloudFlare (for now...) so just using their bandwidth. ;-)

I need to investigate using SourceMaps to help debug CoffeeScript (the Missing Piece in the CoffeeScript Puzzle!)

Must make time for:

License

(The MIT License)

Copyright (c) 2013 @nelsonic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.