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

Responding to GET requests


Adding a simple GET request support is fairly simple, and you've already seen this before in the app we built. Here is some sample code that responds to a GET request and returns a simple JavaScript object as JSON. Insert the following code in the routes section where we have the // TO DO: Setup endpoints ... comment waiting:

router.get('/test', (req, res)=>{ 
    var data = { 
        name: 'Jason Krol', 
        website: 'http://kroltech.com' 
    }; 
    res.json(data); 
}); 

Just like how we set up viewModel in Chapter 5, Templating with Handlebars, we create a basic JavaScript object that we can then send directly as a JSON response using res.json instead of res.render. Let's tweak the function a little bit and change it so that it responds to a GET request against the root URL (that is /) route and returns the JSON data from our movies file. Add this new route after the /test route added previously:

router.get('/', (req, res)=>res.json(json)); 

Note

The...