Chapter 10. Server-Side YUI
For many frontend engineers, running JavaScript on the server is an attractive notion. Wouldn’t it be nice if you didn’t have to write reams of code in JavaScript on the client, and then have to write much of that logic again in some other language for the server?
The good news for those engineers is that in recent years, the practice of writing server-side JavaScript has started to cross over into the mainstream. Clearly it hasn’t become mainstream yet, as we still have to constantly say “server-side JavaScript” instead of just “JavaScript.” Nevertheless, the server offers us the opportunity to write some really interesting JavaScript applications. And as a framework designed to tame JavaScript applications, it should be no surprise that YUI has focused on the server as one of its primary environments.
The specific server-side environment YUI targets is the popular Node.js framework. In a nutshell, Node.js is a JavaScript platform built on top of the V8 JavaScript runtime, with libraries for HTTP and other types of I/O. Just like a browser, Node.js runs in an event loop. And just like the browser, the primary way to pass messages and perform other I/O interactions in Node.js is to set listeners and respond asynchronously with callback functions. For many longtime backend engineers, writing asynchronous code in an event loop feels deeply weird. But for frontend engineers, this model is strikingly familiar.
The main advantage of the event loop is that it ...
Get YUI 3 Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.