A Node.js web app platform recipe

I have good memories from times I was starting with a new technology. From the first "Hello World" to the first "Oh, I get it now" sensation, learning a new platform can really bring back that inspired feeling of empowerment that first sucked you into the esoteric world of software development.

But committing to a new technology has its risks. To really learn a new approach takes time. To gain the benefit that comes from experience, you know you will run into road blocks, and inevitably, you will make mistakes. But in the end, most of us know that in the world of technology, not looking forward carries the most risk of all.

We all wonder- where are things going? But more importantly, we should wonder- what is the direction things ought to go?

Why Node.js?

I first heard about Node.js as many probably have... from secondhand tall tales about some new amazingly fast server platform that uses JavaScript of all things. Tantalizing. And folks who use it seem somehow contented, in a pleasantly bemused sort of way.

I started with Node.js because I was looking for:

  • A new non-IDE server-side solution
  • A server with no state management overhead built in
  • A like-minded developer community
  • A new technology that was gaining acceptance

I adopted Node.js because I found:

  • An amazingly powerful server programming environment
  • Exemplary package management implementation
  • A huge library of very helpful well-written packages
  • A talented helpful community
  • A sparse default configuration - add modules as needed
  • Very low-latency (fast!) response times
  • The magic of the Asynchronous paradigm

Linux vs. Windows

I spent the first 6 months using Node.js in a Windows environment. It works, but you are constantly swimming upstream. Don't fight it. Go Linux. The whole idea of Node.js is a Linux approach. Node is a great excuse to start out with Linux if you've been a good Windows soldier up to now.

When it comes to flavors of Linux, like many others, I quickly realized that Ubuntu is ideal for application hosting. It is also the mostly widely supported and documented OS for this purpose. (I find it less IT and network administrator focused and more developer/app hosting focused than others.)

Node.js Hosting

I skipped more app-centric services such as Heroku. The appliance app hosting approach (such as is also offered by Azure) may be a great approach for scalable mobile app back-end servers. But for more robust solutions, you will want to take advantage of the inherent power Node.js offers through the Linux OS and other software you install on the server. (I also find value in learning on a dedicated VM first, even if you plan to leverage Node specific appliance hosting in the future.)

Azure- I started with Microsoft Azure. At first, this seemed surprisingly nice. The portal seemed simple and elegant, and they seemed to be supporting Node.js well via iisnode. Azure also allows you to set up Linux "Virtual Machines" and install Node yourself. But after a year, I moved away from Azure because:

  • Azure is becoming more complex and Network/IT focused
  • It is more expensive for open source hosting
  • Azure's subscription/payment process is poor
    • It's easy to lose production servers for silly reasons
    • Their customer service is unreliable

Also make sure you understand how Azure throttles your cheaper hosted VM's and cloud databases. I did find the performance of their dedicated VM's to be very consistant though, with reliable network connectivity and up time.

Joyent- I used Joyent for a while:

  • Amazing technology - I plan to use for performance-critical solutions
  • Great team - very supportive and intelligent - wow
  • Note that their SmartOS is custom Linux:
    • Some subtle changes to what you're used to
    • Not easy to re-host elsewhere
    • Worth it if you need speed / advanced performance monitoring
    • Plays VERY well with Node.js
    • A tad more expensive, but great value
    • Billing process is not as mature as it should be

DigitalOcean- I currently use DigitalOcean and have been very impressed:

  • DigitOcean is so simple and elegant - I love it
  • Very cost effective for open source hosting
  • All SSD (Solid-State Drive)
  • Great auto VM backup process
  • Ability to network your virtual servers

Database and DB Access

Reliable relational database interfacing is critical for my endeavors. I have also noticed that relational database use can tend to be under appreciated by newer developers. SQL is more than just storing and retrieving data- it can also be an incredibly powerful element of your process design and functionality.

Microsoft SQL- is cost effective on Azure. In this context, however, you lose some of the benefits of the enterprise product that I have become accustomed to.

MySQL- is cost effective everywhere of course. It was also a fairly easy process to come up to speed with MySQL from MSSQL, and the performance has been great. Enterprise tasks such as backing up and performance monitoring can be a challenge though.

node-sqlserver- node-sqlserver is a Node.js driver for Microsoft SQL server. I spent a fair amount of time trying to get this driver working. They were (and still are...) in the early stages of development, so building the driver is a challenge. I was able to succeed only in an Azure Windows Server VM. It seems like very few (if any) folks have been able to get it working in the Azure "Web Site" appliance environment. And there are no plans yet to create a Linux version. (The Windows Server vision did work quite well for me though.)

node-mysql- node-mysql created by Felix Geisendörfer is a great Node.js module. It is a "pure node.js JavaScript Client implementing the MySql protocol." That it is, and it works wonderfully too. The escape feature is very handy for handling SQL injection issues and automatic date conversion between JavaScript and MySQL (very helpful!). It also supports connection pooling and database transactions.

NoSQL- I have not implemented a major NoSQL project yet - so I can't speak to that. (I know many Node.js platforms tightly integrate NoSQL database support.)

Version Control and Deployment

Coming from the word of Microsoft Visual Source Safe, learning Git was rather intimidating at first. I ended up focusing on the deployment and code management benefits over the version control and team development use. This proved to be a great way to get started with Git - and I could not imagine life without it now.

Email

You will likely need to send emails out from your Node server. I spent longer than I care to recount setting up Postfix SMTP on my VM. In the end, it was not too bad to install and use once I figured it out - but I learned that the IP address you get assigned by your hosting provider is often already blacklisted for email sending - so your emails will end up in the recipient's spam folder. You can clean the IP address, but it takes time and effort.

I took a cue from Ghost and tried Mailgun, a cloud email service created for developers. Mailgun is an amazing service for developers. It not only worked great, but is also very cost effective (free up to 10,000 emails per month.) Their API looks great, they have a simple to use inbound email forwarding filter, great email DNS configuration/troubleshooting tools, and awesome web-based logging.

I used the emailjs Node module to send to Mailgun from my node server with great success.

Running multiple Node.js apps on a single server

At first I stuck with one Node app per server. But as soon as I found myself needing to host several smaller dedicated domain node web sites on one VM, I needed to find a solution. I was used to using IIS with a multi-homed Windows Server to server multiple web sites on one server.

Reverse Proxy approach- Nginx is often used in front of Node.js, not only to host multiple Node sites, but at times to also serve static files and/or implement SSL. I was surprised how easy it was to install and configure Nginx. I still use Node.js to implement SSL and static file serving for the most part, but Nginx has certainly become a key layer in my Node stack now.

Server-side image processing

Getting GraphicsMagick installed and working on the server has allowed me to implement fancy UI functions I never considered before. The Node.js GraphicsMagick wrapper gm makes server-side image processing as easy as possible - although the actual exposed GraphicsMagick commands are rather tricky to get up to speed with, they are incredibly powerful.

Blog integration

If you want to integrate a nice Node.js-based blog into your site, look into Ghost. Ghost is a very new open source blog platform built with Node.js. (You're reading a Ghost blog now.) It uses Handlebars as a template engine and supports custom theme creation. It's quite simple to use their theme approach to embed the whole blog into your existing site design. You can read more about how I set it up here: A Ghost Workflow.


For a full introduction and index to this blog: Node.js: One New Approach

Next post I talk about Starting with Linux for Windows developers.

Cheers!

comments powered by Disqus