Book Image

Learning Node.js for .NET Developers

Book Image

Learning Node.js for .NET Developers

Overview of this book

Node.js is an open source, cross-platform runtime environment that allows you to use JavaScript to develop server-side web applications. This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through understanding the Node.js programming model. You will learn how to build web applications and APIs in Node, discover packages in the Node.js ecosystem, test and deploy your Node.js code, and more. Finally, you will discover how to integrate Node.js and .NET code.
Table of Contents (21 chapters)
Learning Node.js for .NET Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using MongoDB with Express


The games service module in our application currently stores all its data in memory. This worked well enough for demo purposes, but isn't suitable for a real application. We lose all the data whenever the application restarts. It also prevents us from scaling our application across multiple processes. Each instance would have its own game service with different data. Users would see different data depending on which server happened to handle their request.

We're going to update our games service to store its data in MongoDB. For this, we're going to make use of a library called Mongoose.

Persisting objects with Mongoose

Recall that, unlike a relational database, MongoDB does not require documents in the same collection to have the same fields. However, we do typically expect most items within a collection to share at least a common core of fields.

Mongoose is an object modeling library for storing entities in MongoDB. It helps with writing common functionality such...