My first AngularJS web app, part 3

In my last installment, I rounded out the architecture of the Minecraft Free News web app I created for my son. Now let’s talk about the server-side implementation.

I host through AWS and serve via an EC2 instance with a typical LAMP stack installation. However, for this particular project, the MySQL and PHP were not required. Apache is used to serve the web app itself — the HTML, CSS, and JavaScript to the client. Thus, when browsing to on port 80, Apache is doing all the work. Easy enough.

After the client web browser receives the HTML/CSS/JavaScript from Apache, it executes the JavaScript which pulls the enabled RSS feed items from the various Minecraft-related websites. However, the JavaScript I wrote also connects to my server via HTTP on a different port to request a special JSON object. This object contains custom HTML information that can be inserted as the top headline (above the “Minecraft Free News” logo), and/or on any line in any column of the content area. Thus, to make day-to-day content modifications to the web app, I need not edit my JavaScript code served by Apache, but instead modify the JSON object being returned by Node. Note that this JSON object is very small: only about 2 KB.

A Node HTTP server is exceedingly simple to configure in just a few lines, and there are countless tutorials available explaining the details. My version is designed quite closely to the example authored in The Node Beginner Book which explains quite nicely and in some detail the reasoning behind its easily extensible design. So in my case, when the client’s browser executes the JavaScript, it connects to my server on the port which Node is listening, and receives the JSON object. The data from that JSON object is then incorporated in content and rendered by Angular. And viola, the web app is complete.

My first AngularJS web app, part 2

Picking up from my previous installment, I created my web app layout and styling using Bootstrap, heavily customized so it doesn’t look like the standard Bootstrap-built site. Now it’s time to start hanging the meat on the bones by integrating AngularJS and JavaScript. Note that in this and subsequent blog posts I will discuss the high-level design of the app rather than reviewing individual code snippets. Frankly, assuming you can code, I believe it’s more important to understand the design reasoning and notable learnings rather than wading through code to demonstrate syntax.

As you can see on Minecraft Free News, there are essentially two major areas on the page: the feed selection at the very top of the page, and the content (individual feed items) displayed on the rest of the page. After creating the AngularJS module, I created two AngularJS controllers: FeedController to manage the feed source selections at the top, and ContentController to manage populating the feed items into the content area. I created an Angular service that simply provides a data object containing an array of all feed sources including the title, URL, whether the feed is enabled or disabled, and the maximum number of feed items to pull. Via this service, the data object is shared between the FeedController and ContentController. When the user clicks on a feed source to toggle its enable/disable attribute, the specific feed in the data object is changed by the FeedController, and that information is provided to the ContentController which immediately acts upon it to show or hide that feed’s items in the content area. In other words, this service serves as a communication conduit between my two controllers.

I created another Angular service called PullFeeds to interface with Google’s Feed API to pull each individual feed’s content items. This object relies upon Angular promises, an entity bundled with Angular to support asynchronous interactions. As our app is sourcing RSS data from other websites, we are at the mercy of the network and server delays. Per the Google Feed API, we can make a call to pull the feed items from a specific feed URL, and we must supply a call-back function that will process the items received from that feed. Without using a promise, when the PullFeeds service is executed, it will return immediately without any feed items because the action via Google’s Feed API is asynchronous and thus not yet completed. However, when using a promise in the service, only when our call-back function receives the data will the promise be fulfilled and data returned from the service as desired.

I also use promises elsewhere in the app (via another custom service) to pull JSON data from my own server in order to insert custom ads and news items inline in the content. Beyond returning data, promises can also return a rejection that indicates the deferred action failed (i.e. a failed promise to deliver). In my case, if the promise to read the data from my server failed (e.g. the server is down, network inaccessible, etc.), our web app can handle it gracefully by receiving the rejected promise and displaying the feed items without ads.

As a final note for this installment, I want to point out that all of the Javascript execution described thus far occurs on the client side. The HTML, CSS, and Javascript are read by the client from my server, and using AngularJS the resulting page is rendered entirely by the client. All of the feed items are pulled by the client. My server is only used to serve the relatively small HTML/CSS/Javascript, and to supply a small JSON object for custom ad and news items. Though seen as a dynamic web app from the client side, from the server’s perspective it’s all static content and thus very little bandwidth is required.

My first AngularJS web app, part 1

As I mentioned in a previous blog post, I spent the past three months traversing the ecosystem of JavaScript and Node, attempting to learn the language that once mystified me. Along my journey I discovered various MVC frameworks designed to streamline web app development, and I gravitated to AngularJS. As with any programming endeavor, one can only read so much before the call of the command line beckons you to write a real application using everything just learned.

My son, like every other kid in America, is currently a Minecraft fanatic. When he’s not actually playing Minecraft, he is hopping from website to website reading news about the game and watching screencasts from other prominent Minecraft gamers. After some discussion with my son, I decided to built my first web app to aggregate all of the news and video links from the Minecraft websites he likes into a single one-page resource. First, here is the app (using a domain name that was surprisingly available):

In a nutshell, it’s an RSS reader app that pulls feeds from specific feed sources (annotated at the top), sorting and displaying all the articles in reverse chronological order. Any feed source can be enabled or disabled, and that setting is remembered by the browser. The client’s browser, not the web server, pulls all the articles from the selected RSS feeds after the page is loaded. There is an optional top-line article section and ads displayed inline in the content, both of which are pulled from my web server. More on that later.

From a visual design standpoint, you might notice it’s modeled after the styling of DrudgeReport — a site that may look like a 1990’s relic, but in fact accomplishes its goal of delivering links to current news stories with sheer simplicity and readability (read more). I started with Bootstrap and made some modifications via its LESS variables to attain the layout styling I desired. Using Harp as a local web server on my laptop, I included the Bootstrap LESS file and added my own custom LESS file to override specific Bootstrap variables. Harp then automatically compiled the Bootstrap LESS files and my custom LESS file as CSS for each page reload from the Harp web server so I could tweak and view the changes in real time. Once I was satisfied, I saved the resulting Bootstrap CSS to a text file, and that became my Bootstrap CSS for the project. For such a simple design style, using Bootstrap is arguably overkill; however, Bootstrap bought me responsiveness to all types of devices, from laptops to phones, out of the box.

Look for the next installment to learn how I developed the app.