Translated: Two key features:2
- “Event-driven”: in contrast to a ‘thread-based’ model, the web server accepts the request, spins it off to be handled, and then goes on to service the next web request. This is faster than the traditional thread-based model of web apps, which holds a connection open until it has performed the request and/or whatever other transaction was sent.
- “Non-blocking”: see “event-driven. Because the web server is always accepting requests instead of waiting for read or write operations, it is a non-blocking I/O (input/output). That’s faster.3
From the documentation: “Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.”
Translated: Express is a Sinatra-inspired (a comparison to a Ruby framework helps make sense of it) web development framework for node.js. It makes building web apps with Node faster and easier.
From the documentation: “AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML’s syntax to express your application’s components clearly and succinctly. Angular’s data binding and dependency injection eliminate much of the code you currently have to write. And it all happens within the browser, making it an ideal partner with any server technology.”
Translated: Extend HTML’s syntax (Angular is a front end framework) with data binding (it easily and quickly connects the front end interface to the back end database and logic)4 and dependency injection (it makes it easy to pass dependency components to other components).
[WebSockets: “WebSockets represent a standard for bi-directional realtime communication between servers and clients”5]
Translated: WebSockets mean servers/clients can communicate really quickly. As in, in real time. SockJS enables WebSockets or, if that fails, uses other protocols and abstractions to simulate WebSockets. It is one of many WebSocket implementations.6
Inspired by others who have built simple proof-of-concept apps with the above technologies,7 I set out to do the same.
It’s easy enough to get started. Once you have Node downloaded, you can install a lot modules with the
npm install -g express-generator, for example, installs the Express generator, after which
express app-name will generate an Express app. A key file that will be generated is
package.json. Mine looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
npm install is all that’s needed to install those dependencies (basically Ruby’s Bundler for Node).
Beyond that, most of the code is in app.js. There’s a lot more going on to connect SockJS and the front end rendering with Angular (full code on Github), but getting a basic app up and running is very simple. In fact, the 9 lines of code below (which can be refactored to as little as 4 or less)8 are all you need for a Node app (this example does not use Express).
1 2 3 4 5 6 7 8 9 10 11 12 13
This post was also helpful in explaining “why the hell” you might use Node.↩
Helpful analogies about event-driven programming.↩
A couple good ones: Chat Application with Express, SockJS and Angular, A super-simple chat app with AngularJS, SockJS and node.js.↩