Express in Action
eBook - ePub

Express in Action

Evan Hahn

Condividi libro
  1. English
  2. ePUB (disponibile sull'app)
  3. Disponibile su iOS e Android
eBook - ePub

Express in Action

Evan Hahn

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

Express in Action is a carefully designed tutorial that teaches you how to build web applications using Node and Express.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sÏ, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalitĂ  di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in piÚ di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Express in Action è disponibile online in formato PDF/ePub?
SĂŹ, puoi accedere a Express in Action di Evan Hahn in formato PDF e/o ePub, cosĂŹ come ad altri libri molto apprezzati nelle sezioni relative a Informatique e Services Web et API. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2016
ISBN
9781617292422

Part 1. Intro

Welcome to Express in Action. This is the first of three parts and, like many openers, it introduces the players.
In chapter 1, I’ll identify the characters. Spoiler alert: they are Node.js and Express. The former is a JavaScript runtime—a place where JavaScript code can run—that’s attractive to a lot of developers. Node.js is powerful, but its APIs can, at times, lack power and leave you writing a lot of boilerplate code; that’s where Express struts onto the scene. It fits snugly into Node.js and makes it easier to write web applications. You’ll learn all this and much more in chapter 1.
In chapter 2 you’ll learn what it means for Node.js to be a JavaScript runtime, and you’ll be running JavaScript in Node.js. You’ll build a couple of simple modules and then see what it takes to build a website on the platform. You’ll also learn how to include third-party modules from npm, Node.js’s third-party package registry.
The star of the show, Express, takes center stage in chapter 3. You’ll see how Express sits on top of Node.js, and learn its major features. Express will show you convenience after convenience. We’ll delve deeper into each of these features in subsequent chapters, but by the end of chapter 3, you’ll have all of the core Express knowledge you’ll need.
I hope you are as excited as I to get started!

Chapter 1. What is Express?

This chapter covers
  • Node.js, a JavaScript platform typically used to run JavaScript on servers
  • Express, a framework that sits on top of Node.js’s web server and makes it easier to use
  • Middleware and routing, two features of Express
  • Request handler functions
Before we talk about Express, we need to talk about Node.js.
For most of its life, the JavaScript programming language has lived inside web browsers. It started as a simple scripting language for modifying small details of web pages but grew into a complex language, with loads of applications and libraries. Many browser vendors like Mozilla and Google began to pump resources into fast JavaScript runtimes, and browsers got much faster JavaScript engines as a result.
In 2009, Node.js came along. Node.js took V8, Google Chrome’s powerful JavaScript engine, out of the browser and enabled it to run on servers. In the browser, developers had no choice but to pick JavaScript. In addition to Ruby, Python, C#, Java, and other languages, developers could now choose JavaScript when developing server-side applications.
JavaScript might not be the perfect language for everyone, but Node.js has real benefits. For one, the V8 JavaScript engine is fast, and Node.js encourages an asynchronous coding style, making for faster code while avoiding multithreaded nightmares. JavaScript also had a bevy of useful libraries because of its popularity. But the biggest benefit of Node.js is the ability to share code between browser and server. Developers don’t have to do any kind of context switch when going from client and server. Now they can use the same code and the same coding paradigms between two JavaScript runtimes: the browser and the server.
Node.js caught on—people thought it was pretty cool. Like browser-based JavaScript, Node.js provides a bevy of low-level features you’d need to build an application. But like browser-based JavaScript, its low-level offerings can be verbose and difficult to use.
Enter Express, a framework that acts as a light layer atop the Node.js web server, making it more pleasant to develop Node.js web applications.
Express is philosophically similar to jQuery. People want to add dynamic content to their web pages, but the vanilla browser APIs can be verbose, confusing, and limited in features. Developers often have to write boilerplate code, and a lot of it. jQuery exists to cut down on this boilerplate code by simplifying the APIs of the browser and adding helpful new features. That’s basically it.
Express is exactly the same. People want to make web applications with Node.js, but the vanilla Node.js APIs can be verbose, confusing, and limited in features. Developers often have to write a lot of boilerplate code. Express exists to cut down on this boilerplate code by simplifying the APIs of Node.js and adding helpful new features. That’s basically it!
Like jQuery, Express aims to be extensible. It’s hands-off about most parts of your applications’ decisions and is easily extended with third-party libraries. Throughout this book and your Express career, you’ll have to make decisions about your applications’ architectures, and you’ll extend Express with a bevy of powerful third-party modules.
You probably didn’t pick up this book for the “in short” definition, though. The rest of this chapter (and book, really) will discuss Express in much more depth.
Note
This book assumes that you’re proficient in JavaScript but not Node.js.

1.1. What is this Node.js business?

Node.js is not child’s play. When I first started using Node.js, I was confused. What is it?
Node.js (often shortened to Node) is a JavaScript platform—a way to run JavaScript. Most of the time, JavaScript is run in web browsers, but there’s nothing about the JavaScript language that requires it to be run in a browser. It’s a programming language just like Ruby or Python or C++ or PHP or Java. Sure, there are JavaScript runtimes bundled with all popular web browsers, but that doesn’t mean that it has to be run there. If you were running a Python file called myfile.py, you would run python myfile.py. But you could write your own Python interpreter, call it SnakeWoman, and run snakewoman myfile.py. Its developers did the same with Node.js; instead of typing javascript myfile.js, you type node myfile.js.
Running JavaScript outside the browser lets you do a lot—anything a regular programming language could do, really—but it’s mostly used for web development.
Okay, so you can run JavaScript on the server—why would you do this?
A lot of developers will tell you that Node.js is fast, and that’s true. Node.js isn’t the fastest thing on the market by any means, but it’s fast for two reasons.
The first is pretty simple: the JavaScript engine is fast. It’s based on the engine used in Google Chrome, which has a famously quick JavaScript engine. It can execute JavaScript like there’s no tomorrow, processing thousands of instructions a second.
The second reason for its speed lies in its ability to handle concurrency, and it’s a bit less straightforward. Its performance comes from its asynchronous workings.
The best real-world analogy I can come up with is baking. Let’s say I’m making muffins. I have to prepare the batter and while I’m doing that, I can’t do anything else. I can’t sit down and read a book, I can’t cook something else, and so on. But once I put the muffins in the oven, I don’t have to stand there looking at the oven until they’re done—I can do something else. Maybe I start preparing more batter. Maybe I read a book. In any case, I don’t have to wait for the muffins to finish baking for me to be able to do something else.
In Node.js, a browser might request something from your server. You begin responding to this request and another request comes in. Let’s say both requests have to talk to an external database. You can ask the external database about the first request, and while that external database is thinking, you can begin to respond to the second request. Your code isn’t doing two things at once, but when someone else is working on something, you’re not held up waiting.
Other runtimes don’t have this luxury built in by default. Ruby on Rails, for example, can process only one request at a time. To process more than one at a time, you effectively have to buy more servers. (There are, of course, many asterisks to this claim.)
Figure 1.1 demonstrates what this might look like.
Figure 1.1. Comparing asynchronous code (like Node.js) to synchronous code. Note that asynchronous code can complete much faster, even though you’re never executing your code in parallel.
I don’t mean to tell you that Node.js is the fastest in the wo...

Indice dei contenuti