Web Development with Node and Express
Ethan Brown
Dedication
This book is dedicated to my family:
My father, Tom, who gave me a love of engineering;my mother, Ann, who gave me a love of writing;and my sister, Meris, who has been a constant companion.
Foreword
Steve Rosenbaum
President and CEO, Pop Art, Inc.
The combination of JavaScript, Node, and Express is an ideal choice for web teams that want a powerful, quick-to-deploy technology stack that is widely respected in the development community and large enterprises alike.
Building great web applications and finding great web developers isnt easy. Great apps require great functionality, user experience, and business impact: delivered, deployed, and supported quickly and cost effectively. The lower total cost of ownership and faster time-to-market that Express provides is critical in the business world. If you are a web developer, you have to use at least some JavaScript. But you also have the option of using a lot of it. In this book, Ethan Brown shows you that you can use a lot of it, and its not that hard thanks to Node and Express.
Node and Express are like machine guns that deliver upon the silver-bullet promise of JavaScript.
JavaScript is the most universally accepted language for client-side scripting. Unlike Flash, its supported by all major web browsers. Its the fundamental technology behind many of the attractive animations and transitions you see on the Web. In fact, its almost impossible not to utilize JavaScript if you want to achieve modern client-side functionality.
One problem with JavaScript is that it has always been vulnerable to sloppy programming. The Node ecosystem is changing that by providing frameworks, libraries, and tools that speed up development and encourage good coding habits. This helps us bring better apps to market faster.
We now have a great programming language that is supported by large enterprises, is easy-to-use, is designed for modern browsers, and is supplemented with great frameworks and libraries on both client-side and server-side. I call that revolutionary.
Preface
Who This Book Is For
Clearly, this book is for programmers who want to create web applications (traditional websites, RESTful APIs, or anything in between) using JavaScript, Node, and Express. One of the exciting aspects of Node development is that it has attracted a whole new audience of programmers. The accessibility and flexibility of JavaScript has attracted self-taught programmers from all over the world. At no time in the history of computer science has programming been so accessible. The number and quality of online resources for learning to program (and getting help when you get stuck) is truly astonishing and inspiring. So to those new (possibly self-taught) programmers, I welcome you.
Then, of course, there are the programmers like me, who have been around for a while. Like many programmers of my era, I started off with assembler and BASIC, and went through Pascal, C++, Perl, Java, PHP, Ruby, C, C#, and JavaScript. At university, I was exposed to more niche languages such as ML, LISP, and PROLOG. Many of these languages are near and dear to my heart, but in none of these languages do I see so much promise as I do in JavaScript. So I am also writing this book for programmers like myself, who have a lot of experience, and perhaps a more philosophical outlook on specific technologies.
No experience with Node is necessary, but you should have some experience with JavaScript. If youre new to programming, I recommend Codecademy. If youre an experienced programmer, I recommend Douglas Crockfords JavaScript: The Good Parts (OReilly). The examples in this book can be used with any system that Node works on (which covers Windows, OS X, and Linux). The examples are geared toward command-line (terminal) users, so you should have some familiarity with your systems terminal.
Most important, this book is for programmers who are excited. Excited about the future of the Internet, and want to be part of it. Excited about learning new things, new techniques, and new ways of looking at web development. If, dear reader, you are not excited, I hope you will be by the time you reach the end of this book.
How This Book Is Organized
Chapters , you start using Express and build the skeleton of a sample website that will be used as a running example throughout the rest of the book.
cover cookies, sessions, and form handlers, rounding out the things you need to know to build basic functional websites with Express.
explains how to use middleware to send email from the server and discusses security and layout issues inherent to email.
offers a preview into production concerns. Even though, at this stage in the book, you dont have all the information you need to build a production-ready website, thinking about production now can save you from major headaches in the future.
is about persistence, with a focus on MongoDB (one of the leading document databases).
reviews the popular model-view-controller (MVC) paradigm, and how it fits into Express.
discusses security: how to build authentication and authorization into your app (with a focus on using a third-party authentication provider), as well as how to run your site over HTTPS.
explains how to integrate with third-party services. Examples used are Twitter, Google Maps, and Weather Underground.
Chapters talks about the next important (and oft-neglected) phase: maintenance.
The book concludes with , which points you to additional resources, should you want to further your education about Node and Express, and where you can go to get help.
Example Website
Starting in Just having gotten back from a trip to Lisbon, I have travel on my mind, so the example website I have chosen is for a fictional travel company in my home state of Oregon (the Western Meadowlark is the state bird of Oregon). Meadowlark Travel allows travelers to connect to local amateur tour guides, and partners with companies offering bike and scooter rentals and local tours. In addition, it maintains a database of local attractions, complete with history and location-aware services.
Like any pedagogical example, the Meadowlark Travel website is contrived, but it is an example that covers many of the challenges facing real-world websites: third-party component integration, geolocation, ecommerce, performance, and security.
As the focus on this book is backend infrastructure, the example website will not be complete; it merely serves as a fictional example of a real-world website to provide depth and context to the examples. Presumably, you are working on your own website, and you can use the Meadowlark Travel example as a template for it.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold
Shows commands or other text that should be typed literally by the user. Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This element signifies a tip or suggestion.
Note
This element signifies a general note.