Unless you’ve been living under a rock (or perhaps in a van down by the river) you may have noticed that Node.js is kind of a big deal. Since its introduction in late 2009, Node has steadily grown in popularity and now occupies prime real estate as a (if not the) de facto choice of server-side infrastructure for the modern web stack.
Node’s popularity is based on a few distinct advantages relative to its competition. First, Node is fast and very scalable, due to its lightweight hosting model and default pattern of asynchronous I/O (which is a fancy way of saying Node doesn’t wait around doing nothing while that 5 second database query you just issued hasn’t returned yet). Second, in contrast to its power, Node’s core programming model (the concepts needed to understand “the Node way”, and the actual APIs that implement those concepts) is very simple and straightforward. Here is Node’s “hello world” in its entirety:
var http = require('http'); var port = process.env.port || 8888; http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello Worldn'); }).listen(port);
Just a few lines of code, a few more lines of config (not shown), and that’s it! Of course, the complexity ratchets up a few notches once you start building real software… but a surprisingly high percentage of the core Node concepts are found in those few lines. How does your web stack compare? Last, JavaScript is everywhere. JavaScript is the web’s assembly language. And now JavaScript is on the server, too. Node programs are implemented in the same JavaScript you use to write browser-based logic. It executes in the same virtual machine (Chrome’s V8) that many of you are currently using to view this page. If you write JavaScript for the client now, many of the same tips, tricks, open-source libraries, and homespun wisdom you already have in your mental tool belt will work very well in Node. There is a power and utility in having a single language with which to implement your full web application stack.
So far this isn’t terribly controversial, or illuminating… Node is big, its useful, people like it, etc. Gee, thanks Josh. But the devil is in the details. Node may be huge in some highly visible retail corners of the web and in countless small-but-diabolically-fashionable Valley startups, but not all web development occurs in service to the largest retailers on the planet or at the behest of high-brow VCs. So what should the rest of us think about Node?
Here at Wintellect we focus on enterprise web development and talk to a lot of customers across many verticals… energy, engineering, finance, media, etc. Most of them trend toward medium-to-large size in scale, and many (most?) have a variety of technologies (and even technology stacks) at the core of their business. So a key aspect of our consulting is helping customers align their current technology investments with the ever-shifting “industry landscape” and providing context for pending technology decisions. And Node definitely appears on the enterprise radar… for some it’s a buzzword from a magazine article, for others it’s merely tire-kicking. Some are even building on top of it already. But fewer and fewer can afford to ignore it entirely.
Because so many of our enterprise customers are heavily invested in the Microsoft stack, we’ve had time and opportunity to consider “why Node?” in that context. Node has some specific advantages (and even some disadvantages… more on that in another blog post) for the traditional ASP.NET team to consider.
1. Node is a fast, lightweight means to serve static content + JSON
As browser capabilities and JavaScript VMs have improved over the last several years, we’ve seen a steady movement of “application behavior logic” (not necessarily the same thing as business logic) from the server to the client. Where the traditional approach has been to consume server resources to dynamically generate HTML and push it to the browser (Web Forms, Razor, etc.), in the interest of performance and scale (and continuing comfort with JavaScript as an application platform unto itself) we increasingly offload dynamic UI behaviors to the browser through manipulation of “static” HTML via CSS and JavaScript, backfilled by JSON data from the server. Thus today’s web server has comparatively less work to do than yesterday’s… which makes Node’s lightweight model a potentially attractive alternative to ASP.NET’s more heavyweight, prescriptive model that came of age when server-side rendering was state-of-the-art. Ask yourself… if you’re primarily serving static HTML, CSS, and JavaScript files + JSON data from REST endpoints, do you really need ASP.NET for that?
2. Node is a great choice for teams with extensive JavaScript skills investments
Certainly if you’re a .NET developer then you’ve invested heavily in the Microsoft stack and the requisite skillsets to maximize that. But the days of the monochromatic enterprise are gone, and even we C# diehards have had to (sometimes grudgingly ) bolster our JavaScript skills in order to stay current. If your team is staffed with a bunch of JS rockstar-ninja-samauri-megacoders then by all means take a closer look at Node for your next project. The ability to share tribal wisdom, libraries, and best practices across your entire application codebase and team is very powerful. Ironically we’ve been there before in the not-too-distant past… this time it appears we’ll have a different outcome.
3. Node integrates very nicely with Visual Studio
In November 2013 Microsoft released the Node.js Tools for Visual Studio, which do exactly what you’d expect from the name… provide an integrated development and (importantly) debugging experience for Node applications, right inside VS.NET. You get breakpoints, variable values, call stacks, editor niceties, NPM integration (NPM is NuGet for Node, in essence), and more. It’s an alpha release, but very usable. Especially if you’re planning to continue .NET development in addition to exploring Node, I highly recommend it.
Check out this brief video tour from the team who built it:
[youtube https://www.youtube.com/watch?v=W_1_UqUDx2s]
4. Node has a burgeoning ecosystem for Windows and .NET integration
As a long-time .NET developer, this has been the most pleasant surprise (and my favorite part) of getting to know Node. There are a handful of Node modules (opt-in extensions and frameworks that augment Node’s core behaviors) that make transitioning from .NET to Node less painful, even comfortable. If you know .NET and you’re getting to know Node, make time for these as well. In no particular order:
iisnode – This module allows you to host Node applications inside IIS on Windows (Node still runs in its own process(es) so it’s not quite “inside”). This conveys several useful advantages to your Node apps… IIS process lifetime management, support for web gardens (important for scaling Node on multi-core machines), integrated security, etc.
httpsys – This is a drop-in replacement for Node’s built-in HTTP stack. It utilizes the kernel-mode http.sys driver in Windows to provide increased performance and kernel-level output caching (among others). Not compatible with iisnode (so, useful for self-hosted Node apps on Windows).
node-sqlserver – A module developed by the Azure team at Microsoft for querying against local or cloud-based SQL Server instances, from within a Node app. Not as wrist-friendly as Entity Framework but certainly gets the job done.
passport-windowsauth – This module provides Integrated Windows authentication for Node applications, similar to the traditional integrated auth experience in ASP.NET. It also does forms-based authentication. It depends upon the passport.js Node authentication module.
TypeScript – Not a Node module per-se, but its worth noting that TS provides a great experience for building and maintaining Node applications. Especially useful for C# devs moving to Node and struggling to build out large-ish codebases.
edge – Edge.js is my favorite module for Node/.NET integration. Edge allows you to call virtually any arbitrary .NET code from within a Node application. Any .NET code you can shoehorn into an async delegate is usable. This is great for integrating with existing .NET code libraries… you can write new Node applications and still leverage valuable core business functionality written in .NET, without having to re-write it in JavaScript! Very powerful stuff. If you’re coming from .NET and going to Node, check it out.
5. Node works great with Windows Azure
Whether you’re talking about IaaS Virtual Machines or PaaS Web Sites or Cloud Services, Node.js deploys easily and runs great on the Azure cloud platform. To help you get started, the Azure folks provide a comprehensive, Node-centric overview on how to target the various platform services like Tables, Queues, Service Bus, etc. Node+Azure isn’t seen too often in the wild (at least in the enterprise arena that Wintellect occupies) but that’s a shame… it’s a very powerful combination. You get the simplicity of the Node app model with the feature set and dynamic performance and scale of a major cloud infrastructure. What’s not to like about that? Oh, and by the way… Microsoft themselves liked Node well enough that they based their initial Azure Mobile Services infrastructure on it (they’ve since implemented a .NET version, as well).
Here’s an animation showing how easy it is to deploy from VS.NET to Azure Web Sites:
To be fair, Node has weaknesses to consider as well, just like any other web development stack. And transitioning from ASP.NET to Node isn’t always a sure win… ASP.NET hasn’t exactly stood still during Node’s rise to prominence. The last few years we’ve seen the addition of Web API for building RESTful services, better support for rich browser frameworks, and efforts to standardize the interfaces between components of the modern web stack (OWIN). And even greater things are in store from Redmond. I’ll have more to say on reasons to stick with ASP.NET over Node in a follow up post.
That said, Node is an undeniably interesting and powerful technology. So if I’ve piqued your interest then I’d urge you to take a closer look at what Node has to offer and how it might play a role in your next project. For more information about how Node fits into a .NET developer’s world, see my video “Node.js For the Confused – A Practical Guide For .NET Developers” on WintellectNOW. Use promo code Lane-13 for a free trial.