Book Image

Web Development with MongoDB and Node - Third Edition

Book Image

Web Development with MongoDB and Node - Third Edition

Overview of this book

Node.js builds fast, scalable network applications while MongoDB is the perfect fit as a high-performance, open source NoSQL database solution. The combination of these two technologies offers high performance and scalability and helps in building fast, scalable network applications. Together they provide the power for manage any form of data as well as speed of delivery. This book will help you to get these two technologies working together to build web applications quickly and easily, with effortless deployment to the cloud. You will also learn about angular 4, which consumes pure JSON APOIs from a hapi server. The book begins by setting up your development environment, running you through the steps necessary to get the main application server up-and-running. Then you will see how to use Node.js to connect to a MongoDB database and perform data manipulations. From here on, the book will take you through integration with third-party tools to interact with web apps. You will see how to use controllers and view models to generate reusable code that will reduce development time. Toward the end, the book supplies tests to properly execute your code and take your skills to the next level with the most popular frameworks for developing web applications. By the end of the book, you will have a running web application developed with MongoDB, Node.js, and some of the most powerful and popular frameworks.
Table of Contents (19 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Sharing and reusing JavaScript


While you develop web applications, you will always end up doing the validations for your UI, both at the client and server sides, as the client-side validations are required for better UI experience and server-side validations are needed for better security of the app. Think about two different languages in action: you will have the same logic implemented in both the server and client sides. With Node.js, you can think of sharing the common function between server and client, reducing the code duplication to a large extent.

Ever worked on optimizing the load time for client-side components of your Single-Page Application (SPA) loaded from template engines such as Underscore? You would end up thinking about a way we could share the rendering of templates in both server and client at the same time; some call it hybrid templating.

Node.js resolves the context of duplication of client templates better than any other server-side technologies, just because we can use the same JS templating framework and the templates both at server and client.

If you are taking this point lightly, the problem it resolves is not just the issue of reusing validations or templates on the server and client. Think about an SPA being built; you will need to implement the subsets of server-side models in the client-side MV* framework also. Now, think about the templates, models, and controller subsets being shared on both client and server. We are solving a higher scenario of code redundancy.

Not just for building web servers!

Node.js is not just to write JavaScript in the server-side. Yes, we have discussed this point earlier. Node.js sets up the environment for the JavaScript code to work anywhere it can be installed. It can be a powerful solution to create command-line tools, as well as fully featured, locally run applications that have nothing to do with the web or a browser. Grunt.js is a great example of a Node-powered command-line tool that many web developers use daily to automate tasks such as build processes, compiling CoffeeScript, launching Node.js servers, running tests, and more.

In addition to command-line tools, Node.js is increasingly popular among the hardware crowd with the Node.js bots movement. Johnny-Five and Cylon.js are two popular Node.js libraries that exist to provide a framework to work with robotics. Just search on YouTube for Node.js robots and you will see a lot of examples. Also, there is a chance that you might be using a text editor developed on Node.js. GitHub's open source editor named Atom, which is hugely popular, is an example.

Real-time web application with Socket.io

One of the important reasons behind the origin of Node.js was to support real-time web applications. Node.js has a couple of frameworks built for real-time web applications which are hugely popular: Socket.io and Sock.JS. These frameworks make it very simple to build instant, collaboration-based applications such as Google Drive and Mozilla's together.js. Before the introduction of WebSockets in the modern browsers, this was achieved via long polling, which was not a great solution for real-time experience. While WebSockets is a feature that is only supported in modern browsers, Socket.io acts as a framework, which also features seamless fallback implementations for legacy browsers.

Note

If you need to understand more on the use of WebSockets in applications, here's a good resource on MDN that you can explore:https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications.

Networking and file IO

In addition to the powerful non-blocking asynchronous nature of Node.js, it also has robust networking and filesystem tools available via its core modules. With Node.js's networking modules, you can create server and client applications that accept network connections and communicate via streams and pipes. Node contains a module named as fs or filesystem which is totally responsible for all kind of read write operations performed on files. It also takes an advantage of streaming feature of node to perform those operations.

Microservices

Dividing the app with respect to a functionality unit is called a microservice. Each microservice becomes the self contained unit of deployment. Node.js is based on the common JS modules pattern which provides modularity in structure of an application. Such a pattern is used to create the microservices. With the increase of functionality, the number of microservices increases. To manage those services, the Node.js ecosystem provides powerful libraries like pm2. Therefore, it enables the elements of an application to be updated and scaled separately.

Internet of things (IoT)

With the advent of Internet of things (IoT), the Node.JS ecosystem provides amazing library support for various devices like sensors, beacons, wearables, and so on. Node.js is considered as an ideal technology for managing the request made by those devices via its powerful backbone of streams and non-blocking I/O. Popular IoT board variants like Arduino, Raspberry Pi, and so on, have more than 300 Node.js packages. The developers building data-intensive, real-time applications often find Node.js as a natural fit.