22 Slices
Medium 9780992461256

13. Using Express in Our App

Colin J Ihrig SitePoint ePub

This chapter revisits the example Human Resources application that was started in Chapter 6 and continued in Chapter 9. Up to this point, we've created a Node application that communicates with a MongoDB store. We used Node's http module to write a custom implementation for a router. Now that we have the more powerful Express router at our disposal, we're going to replace our http code.

If we were starting a new application from scratch, we would likely want to use a scaffolding generator, such as Yeoman (which has a MEAN stack generator); however, since we're updating our existing application, we're going to modify everything by hand.

The first step in our migration to Express is to update the package.json file. We need to install Express and several pieces of middleware. We can install all the necessary modules using the command in Listing 13-1:

Listing 13-1. Installing Express and other required middleware

See All Chapters
Medium 9780992461256

4. Node’s Programming Model

Colin J Ihrig SitePoint ePub

A language’s programming model defines how developers write code, and can make or break a language. Since Node is just JavaScript, the rules of the language are the same; however, Node does adhere to a number of additional conventions that make code consistent across its core and third-party modules. In Chapter 2, you learned how to create a simple HTTP server in Node. The goal of this chapter is to help you better understand the code structure of that server so you can extend it to other Node applications.

The most important concept to understand about JavaScript, and Node by extension, is that it is single-threaded. This means that JavaScript applications can only perform one task at a time. They can, however, give the illusion of being multi-threaded through the use of an event loop. Essentially, the JavaScript engine maintains several queues of unhandled tasks. These queues include things such as events, timers, intervals, and immediates. Each execution of the event loop, known as a cycle, causes one or more tasks to be dequeued and executed. As these tasks execute, they can add more tasks to the internal queues. Each cycle is made up of smaller steps, known as ticks. An example of a tick would be the act of accessing an item from timer queue.

See All Chapters
Medium 9780992461256

18. Controllers

Colin J Ihrig SitePoint ePub

Controllers create a framework to promote code reusability and testability. They are the basic building blocks of an Angular application. In previous chapters, we covered data binding and directives. We briefly touched on Angular controllers at the end of Chapter 16. In this chapter, we’re going to cover controllers in greater detail. We’ll discuss what they are and what purpose they serve; we will then cover some syntax issues, best practices, and examples.

Like everything related to Angular, it is important to remember that controllers are just JavaScript functions. These functions are used to create a new Angular (or scope) context and are bound to sections of HTML markup. A controller provides a place to put application and view logic that is specific to a particular set of HTML tags. Keep in mind that a single view can use multiple controllers: the view to controller relationship is not one-to-one. It is also possible to reuse controllers in different views.

See All Chapters
Medium 9780992461256

2. Node.js Introduction

Colin J Ihrig SitePoint ePub

To date, there have been over ten different server-side implementations of JavaScript. While Node.js is the most successful, it is far from being the first. Engineers have spent considerable time and effort trying to make JavaScript run on a web server—but why? What is it about JavaScript that makes it so well-suited to being a server-side language? Ultimately, it boils down to two factors: familiarity and non-blocking asynchronous I/O.

Looking at GitHub usage, JavaScript is the prevailing language. As evidenced in Figure 2.1, the raw amount of JavaScript code continues to grow, outpacing all the other popular scripting languages available today. The graph represents the number of new GitHub repositories created that list JavaScript as the primary language.

Figure 2.1. New GitHub repositories by language

JavaScript was created in 1995 by Brendan Eich during his time at Netscape Communications Corporation. By 1996, the official ECMA specification had been submitted with both Microsoft and Netscape implementing JavaScript in their flagship browsers. Over the subsequent twenty years or so, there have been new versions of the ECMAScript specification, JavaScript libraries such as jQuery, client-side JavaScript MVC frameworks, JavaScript controlling robots with Tessel, and the rise of server-side JavaScript. If you have ever worked on a web page in the last twenty years, you’ve had some exposure to JavaScript.

See All Chapters
Medium 9780992461256

17. Angular Directives

Colin J Ihrig SitePoint ePub

This chapter continues our exploration of Angular’s core principles. Specifically, this chapter focuses on directives, which are what allow Angular to extend HTML syntax. Angular ships with a number of built-in directives, but developers can also define custom directives to suit their application’s needs. This chapter will explain how to use directives, as well as how you can create your own.

Let’s begin by defining exactly what is a directive. According to the official documentation:

At a high level, directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS’s HTML compiler ($compile) to attach a specified behavior to that DOM element or even transform the DOM element and its children.

In other words, directives are additional pieces of markup that Angular interprets to extend the default behavior of HTML. Directives can be specified as custom HTML elements, attributes, classes, or comments. For example, given a fictional directive named ngFoo, Listing 17-1 shows how it can be used as an element, attribute, class, and comment:

See All Chapters

See All Slices