eBook - ePub
Express in Action
Evan Hahn
This is a test
Share book
- English
- ePUB (mobile friendly)
- Available on iOS & Android
eBook - ePub
Express in Action
Evan Hahn
Book details
Book preview
Table of contents
Citations
About This Book
Express in Action is a carefully designed tutorial that teaches you how to build web applications using Node and Express.
Frequently asked questions
How do I cancel my subscription?
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoâs features. The only differences are the price and subscription period: With the annual plan youâll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weâve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Express in Action an online PDF/ePUB?
Yes, you can access Express in Action by Evan Hahn in PDF and/or ePUB format, as well as other popular books in Informatique & Services Web et API. We have over one million books available in our catalogue for you to explore.
Information
Topic
InformatiqueSubtopic
Services Web et APIPart 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...