Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes.
These will be cleaned up during production of the book by copyeditors and proofreaders.
welcome
Thank you so much for purchasing the MEAP for Express in Action ! The book is really coming along, and I'm honored that you'll join me as I continue to work on it. This is an intermediate-level book aimed at intermediate-level JavaScript developers who want to start building web applications with Express and Node.js.
I've tried to make the content approachable to Node.js beginners. I want to emphasize how Express works , not just how to use it; I find this thinking invaluable when debugging and when deciding how to architect my software. I also hope to show how to extend Express with other tools to build rich web applications.
In this MEAP, we've got seven chapters of the planned eleven and an appendix.
The first chapter outlines Express at a high level, and is aptly-named "What is Express?". It explains what the Express framework is and isn't, its strengths and weaknesses, and its major conceptual hurdles.
Chapter 2 aims to give you a solid understanding of Express's foundations. It starts by focusing on low-level Node.js, and then transitions to showing how to do the same things with Express at a higher level. By the end of the chapter, you'll have a strong understanding of the framework which we'll build on for the rest of the book.
Chapter 4 digs into routing, which is one of Express's major features. Routing allows Express to appropriately sort requests coming into your website. The chapter starts with basic examples but gets in-depth. If any of the basic web workings confuse you, you can take a look at the appendix that describes HTTP in detail.
Chapter 5 talks about dynamically-generated HTML templates. We'll try both EJS and Jade, two popular templating engines for Node.
Chapter 6 is a chapter about databases. There are very few web applications that can function without some kind of data storage, and this chapter tackles that with the MongoDB database, a popular choice for Express developers.
Chapter 8 will have you setting up automated testing for your Express applications. After you write automated tests, you'll be much more confident that your code is robust and performing as it should. We'll look at some of the helpful tools out there.
Chapter 10 takes your applications to the real worldyou'll deploy your applications to the Heroku cloud platform. You'll be able to write Express applications and show them to the world. This chapter also discusses compilation of assets, which includes the packaging of JavaScript and CSS to speed up your client-side resources.
Future chapters will discuss middleware in detail, how to secure Express applications, techniques for building APIs, and how to integrate Express with databases and real-time features.
I really hope you'll continue to give feedback in the Author Online forum. I'll be reading and responding to all of your comments, and your feedback is very helpful as I work on this book!
Thanks again! Honored that you'd join me on this journey.
Evan Hahn
1 What is Express?
Before we talk about Express, we need to talk about Node.js.
For most of its life, the JavaScript programming language has lived inside of web browsers. It started as a simple scripting language for modifying small details of webpages, but grew into a complex language, with loads of applications and libraries. Many browser vendors like Mozilla and Google began to pump lots of resources into fast JavaScript runtimes, and Google Chrome and Mozilla Firefox got much faster JavaScript engines as a result.
In 2009, Node.js came around. Node took V8, Google Chrome's powerful JavaScript engine, out of the browser and made it able to run on servers. In the browser, developers had no choice but to choose JavaScript. In addition to Ruby, Python, C#, Java, or other languages, developers could now choose JavaScript when developing server-side applications.
JavaScript might not be the perfect language for everyone, but Node.js had some real benefits. For one, the V8 JavaScript engine is fast, and Node encourages an asynchronous coding style, making for faster code while avoiding multi-threaded 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 switching between client and server. Now they can use the same code and the same coding paradigms between two different JavaScript runtimes; the browser and the server.
Node.js caught onpeople 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 offerins can be a bit verbose and difficult.
Enter Express.js. Express is a framework that acts as light layer on top of the Node.js web server, making it more pleasant to develop Node.js web applications.
Express.js is philosophically similar to jQuery. People want to add dynamic content to their webpages, but the vanilla browser APIs can be verbose, confusing, and limited in features. Developers often have to write a lot of boilerplate code. 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 are proficient in JavaScript, but not Node.js.
1.1 What is this Node.js business?
Node.js is no child's play.
When I first started using Node.js, I was confused. What is it?
Node.js (often shortened to "Node") is just 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