APIs are at the core of every serious web application. Node.js is an especially exciting tool that is easy to use, allows you to build APIs, and develop your backend code in JavaScript. It powers the server side of web apps, including PayPal, Netflix, and Zenhub.
Express.js is the most popular framework that can be used to build on top of Node.js—it provides an essential level of abstraction to develop robust web applications. With the emergence of this minimal and flexible Node.js web application framework, creating Node.js applications has become much simpler, faster, and also requires minimal effort.
This book takes a pragmatic approach to leveraging what Express.js has to offer, introduces key libraries, and fully equips you with the skills and tools necessary to build scalable APIs from start to finish while offering subtle details and nuggets of wisdom that come from years of experience.
Chapter 1, Building a Basic Express Site, will provide a basic application (scaffolding), which we will use for the upcoming examples. You will get an insight into what Express applications look like.
Chapter 2, A Robust Movie API, will walk you through building a movie API that allows you to add actor and movie information to a database and connect actors to movies and vice versa.
Chapter 3, Multiplayer Game API – Connect 4, will revolve around building a multiplayer game API. We will also build the app using test-driven development with maximum code coverage.
Chapter 4, MMO Word Game, will teach you how to build a real-time application with Express and SocketIO, perform authentication for socket handshaking, and deal with race conditions using MongoDB's atomic update.
Chapter 5, Coffee with Strangers, will enable you to write an API that allows users to go for a coffee! It will comprise a simple, yet extendable user-matching system.
Chapter 6, Hacker News API on Koa.js, will take you through building a CRUD backend to post links and upvote on Koa.js. We will also look at centralized error handling and avoid callback hell with thunks.
Appendix, Connect 4 – Game Logic, shows the accompanying game logic that we omitted in Chapter 3, Multiplayer Game API – Connect 4.
You'll need the following to get started with the examples in this book:
MongoDB: https://www.mongodb.org/downloads
RoboMongo: http://robomongo.org/
Mocha: Use the
npm i -g mocha
command to download it
Mac OS is preferred but not a necessity.
This book is for beginners to Node.js and also for those who are technically advanced. By the end of this book, every developer will have the expertise to build web applications with Express.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "If it is, then we render the users/profile.jade
template with req.user
as the data."
A block of code is set as follows:
var express = require('express'); var app = express(); app.get('/', function(req, res, next) { res.send('Hello, World!'); }); app.listen(3000); console.log('Express started on port 3000');
Any command-line input or output is written as follows:
$ npm install --save express
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can also right click on the page, and select Inspect Element."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.