Book Image

Express.js Blueprints

By : Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang
Book Image

Express.js Blueprints

By: Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang

Overview of this book

<p>APIs are at the core of every serious web application. Express.js is the most popular framework for building on top of Node.js, an exciting tool that is easy to use and allows you to build APIs and develop your backend in JavaScript. Express.js Blueprints consists of many well-crafted tutorials that will teach you how to build robust APIs using Express.js.</p> <p>The book covers various different types of applications, each with a diverse set of challenges. You will start with the basics such as hosting static content and user authentication and work your way up to creating real-time, multiplayer online games using a combination of HTTP and Socket.IO. Next, you'll learn the principles of SOA in Node.js and see them used to build a pairing as a service. If that's not enough, we'll build a CRUD backend to post links and upvote with Koa.js!</p>
Table of Contents (14 chapters)

Schema design


Each player can simply be represented by a document with a single field for the name:

{ name: 'leo' }

User schema

We will use Mongoose for our data modeling. Let's start with designing our user schema. The schemas are placed in the models folder in the app. The following screenshot shows the folder structure. The schema will have one required field name, this is done by adding required: true to the name object in the schema.

To make querying a user by name fast, we can add an index to name. By default, only the _id field that MongoDB generates will be indexed. This means, to perform a search by name, the database will need to iterate over all the documents in the collection to find a matching name. When you add an index to name, you can query by name as quickly as when you query by _id. Now, when a user leaves, we can find the user by name directly and remove that user.

Also, we add the unique : true property to the index to avoid having multiple users with the same name, as shown...