Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Advanced Node.js Development
Advanced Node.js Development

Advanced Node.js Development: Master Node.js by building real-world applications

eBook
₱1256.99 ₱1796.99
Paperback
₱2245.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Advanced Node.js Development

MongoDB, Mongoose, and REST APIs – Part 1

In this chapter, you're going to learn how to connect your Node applications to the MongoDB database you've been running on your local machine. This means that we'll be able to issue database commands right inside of our Node apps to do stuff like insert, update, delete, or read data. This is going to be critical if we're ever going to make that Todo REST API. When someone hits one of our API endpoints, we want to manipulate the database, whether it's reading all of the Todos or adding a new one. Before we can do any of that though, we have to learn the basics.

Connecting to MongoDB and writing data

To connect to our MongoDB database from inside of Node.js, we're going to be using an npm module created by the MongoDB team. It's called node-mongodb-native, but it includes all of the features you'll need to connect to and interact with your database. To get to it, we're going to Google node-mongodb-native:

The GitHub repo, which should be the first link, is the one we wantthe node-mongodb-native repositoryand if we scroll down, we can take a look at a few important links:

First up we have documentation, and we also have our api-docs; these are going to be critical as we start exploring the features that we have inside of this library. If we scroll down further on this page, we'll find a ton of examples on how to get started. We'll be going through a lot of this stuff in this chapter, but...

The ObjectId

Now that you have inserted some documents into your MongoDB collections, I want to take a moment to talk about the _id property in the context of MongoDB because it's a little different than the IDs that you're probably used to if you've used other database systems, like Postgres or MySQL.

The _id property in the context of MongoDB

To kick off our discussion of the _id property, let's go ahead and rerun the mongodb-connect file. This is going to insert a new document into the Users collection, like we've defined in the db.collection line. I'm going to go ahead and do that by running the file through the node. It's in the playground folder, and the file itself is called mongodb...

Fetching data

Now that you know how to insert data into your database, let's go ahead and talk about how we can fetch data out of it. We're going to be using this technique in the Todo API. People are going to want to populate a list of all the Todo items they need, and they might want to fetch the details about an individual Todo item. All of this is going to require that we can query the MongoDB database.

Fetching todos in Robomongo file

Now, we're going to create a new file based off of mongodb-connect. In this new file, instead of inserting records, we'll fetch records from the database. I'm going to create a duplicate, calling this new file mongodb-find, because find is the method we're going...

Setting up the repo

Before we go any further, I do want to add version control to this project. In this section, we're going to create a new repo locally, make a new GitHub repository, and push our code to that GitHub repository. If you're already familiar with Git or GitHub, you can go ahead and do that on your own; you don't need to go through this section. If you're new to Git and it doesn't make sense just yet, that's also fine. Simply follow along, and we'll go through the whole process.

This section is going to be really simple; nothing MongoDB- related here. To get started, I am going to go ahead and initialize a new Git repository from the Terminal by using git init. This is going to initialize a new repository, and I can always run git status like this to take a look at the files that are untracked:

Here we have our playground folder...

Deleting documents

In this section, you're going to learn how to delete documents from your MongoDB collections. Before we get into that, in order to explore the methods that let us delete multiple documents or just one, we want to create a few more Todos. Currently, the Todos collection only has two items, and we're going to need a few more in order to play around with all these methods involving deletion.

Now, I do have two. I'm going to go ahead and create a third by right-clicking and then going to Insert Document.... We'll make a new document with a text property equal to something like Eat lunch, and we'll set completed equal to false:

{
   text: 'Eat lunch',
   completed: false
}

Now before we save this, I am going to copy it to the clipboard. We're going to create a few duplicate Todos so we can see how we can delete items based...

Updating data

You know how to insert, delete, and fetch documents out of MongoDB. In this section, you're going to learn how to update documents in your MongoDB collections. To kick things off, as usual, we're going to duplicate the last script we wrote, and we'll update it for this section.

I'm going to duplicate the mongodb-delete file, renaming it to mongodb-update.js, and this is where we'll write our update statements. I'm also going to delete all of the statements we wrote, which is the deleted data. Now that we have this in place, we can explore the one method we'll be looking at in this section. This one is called findOneAndUpdate. It's kind of similar to findOneAndDelete. It lets us update an item and get the new document back. So if I update a Todo, set it as completed equal to true, I will get that document back in the response...

Connecting to MongoDB and writing data


To connect to our MongoDB database from inside of Node.js, we're going to be using an npm module created by the MongoDB team. It's called node-mongodb-native, but it includes all of the features you'll need to connect to and interact with your database. To get to it, we're going to Google node-mongodb-native:

The GitHub repo, which should be the first link, is the one we want—the node-mongodb-native repository—and if we scroll down, we can take a look at a few important links:

First up we have documentation, and we also have our api-docs; these are going to be critical as we start exploring the features that we have inside of this library. If we scroll down further on this page, we'll find a ton of examples on how to get started. We'll be going through a lot of this stuff in this chapter, but I do want to make you aware of where you can find other resources because the mongodb-native library has a ton of features. There are entire courses dedicated to...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Entirely project-based and practical
  • • Explains the "why" of Node.js features, not just the "how", providing with a deep understanding and enabling you to easily apply concepts in your own applications
  • • Covers the full range of technologies around Node.js – npm, MongoDB, version control with Git, and many more

Description

Advanced Node.js Development is a practical, project-based book that provides you with all you need to progress as a Node.js developer. Node is a ubiquitous technology on the modern web, and an essential part of any web developer’s toolkit. If you're looking to create real-world Node applications, or you want to switch careers or launch a side-project to generate some extra income, then you're in the right place. This book was written around a single goal: turning you into a professional Node developer capable of developing, testing, and deploying real-world production applications. There's no better time to dive in. According to the 2018 Stack Overflow Survey, Node is in the top ten for back-end popularity and back-end salary. This book is built from the ground up around the latest version of Node.js (version 9.x.x). You'll be learning all the cutting-edge features available only in the latest software versions. This book delivers advanced skills that you need to become a professional Node developer. Along this journey you'll create your own API, you'll build a full real-time web app and create projects that apply the latest Async and Await technologies. Andrew Mead maps everything out for you in this book so that you can learn how to build powerful Node.js projects in a comprehensive, easy-to-follow package designed to get you up and running quickly.

Who is this book for?

This book is for anyone looking to launch their own Node applications, switch careers, or freelance as a Node developer. You should have a basic understanding of JavaScript in order to follow this book. This book follows directly on from Learning Node.js Development, but more advanced readers can benefit from this book without having read the first part.

What you will learn

  • • Develop, test, and deploy real-world Node.js applications
  • • Master Node.js by building practical, working examples
  • • Use awesome third-party Node modules such as MongoDB, Mongoose, Socket.io, and Express
  • • Create real-time web applications
  • • Explore async and await in ES7

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 30, 2018
Length: 592 pages
Edition : 1st
Language : English
ISBN-13 : 9781788393935
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Mar 30, 2018
Length: 592 pages
Edition : 1st
Language : English
ISBN-13 : 9781788393935
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 6,430.97
Node.js Web Development
₱2500.99
RESTful Web API Design with Node.js 10, Third Edition
₱1683.99
Advanced Node.js Development
₱2245.99
Total 6,430.97 Stars icon
Banner background image

Table of Contents

11 Chapters
Getting Set Up Chevron down icon Chevron up icon
MongoDB, Mongoose, and REST APIs – Part 1 Chevron down icon Chevron up icon
MongoDB, Mongoose, and REST APIs – Part 2 Chevron down icon Chevron up icon
MongoDB, Mongoose, and REST APIs – Part 3 Chevron down icon Chevron up icon
Real-Time Web Apps with Socket.io Chevron down icon Chevron up icon
Generating newMessage and newLocationMessage Chevron down icon Chevron up icon
Styling Our Chat Page as a Web App Chevron down icon Chevron up icon
The Join Page and Passing Room Data Chevron down icon Chevron up icon
ES7 classes Chevron down icon Chevron up icon
Async/Await Project Setup Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.5
(6 Ratings)
5 star 33.3%
4 star 16.7%
3 star 16.7%
2 star 33.3%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Martin Richard Sanders Nov 05, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
He is putting me on promotion, because of this book.
Amazon Verified review Amazon
David Leonardo Galasso Sep 01, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Gratefull reading, I've enjoyed every page.Get the match if you are starting back-end from Javascript!Good book
Amazon Verified review Amazon
3xpl0it2c Oct 04, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Very good,but I think it is very basic...All I have learned is mongoose, async and socket.ioIt does teach well but I expected More.
Amazon Verified review Amazon
S. Olson Feb 18, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Why would I buy this title whent he publisher doesn't include a TOC? Tells a story that isn't very promising.
Amazon Verified review Amazon
Anastasios Apr 22, 2024
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
A lot of typos on the code. Needs to be updated. Also try on write the code inside a code bracket if its fully done or do piece by piece code not both, it makes it really confusing.
Subscriber review Packt
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.