Exericse M1: creating a /getAllOrders NodeJS+Express app URI to retrieve and display all documents in ORDERS collection
follow the following steps to create Controller code (controllers/database.js) that access ORDERS collection in a MongoDB on mLab.com and forwards these documents to the view views/getAllOrders.ejs where it cycles through and formats the Order documents in a nice HTML format. The controller code will be invoked by typing in the URI with path = /getAllOrders
YOU WILL BE ASKED to demo the application in person on the due date during class when called.
Your database.js file will contain an exported function named getAllOrders that will connect to your mLab DB (you will need to CHANGE your url) and make a query to retrieve all the ORDERS --- I am not going to write this code (we have same nodeJS and MongoDB code off our outline you should look at) --but , here is some VERY close looking code that connects to a mLabDB and gets all the documents in the Routes collection.... here the name of the function is getAllRoutes
var mongodb = require('mongodb'); |
I am not going to write this code (we have same nodeJS and MongoDB code off our outline you should look at) --but , here is some VERY close looking code that maps the /getAllRoutes URI to the function getAllRoutes in the controllers/database.js file
var express = require('express');
var router = express.Router();
//LOAD the various controllers
var controllerMain = require('../controllers/main'); //this will load the main controller file
var controllerMongoCollection = require('../controllers/database'); //load controller code dealing with database mongodb and Routes collection
//MAY HAVE OTHER CODE in index.js //CODE to route /getAllRoutes to appropriate Controller function //**************************************************************************
//***** mongodb get all of the Routes in Routes collection w // and Render information iwith an ejs view
router.get('/getAllRoutes', controllerMongoCollection.getAllRoutes);
I am not going to write this code (we have same nodeJS and MongoDB code off our outline you should look at) --but , here is some VERY close looking code that is the file views/getAllRoutes.ejs that is invoked by the getAllRoutes controller (in database.js) that is passed an array of Routes documents it cycles through and places inside the ejs html formatted view
views/getAllRoutes.ejs <!DOCTYPE html>
<html>
<head>
<title>mLab MongoDB test</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<div class="container">
<h2>Database Results getting Routes </h2>
<ul>
<% results.forEach(function(r) { %>
<li><%= r.name %> - <%= r.frequency %></li>
<ul>
<li>From:( <%=r.START_Longitude %>, <%=r.START_Latitude %> )</li>
<li>TO: ( <%=r.END_Longitude %>,<%=r.END_Latitude %> ) </li>
</ul>
<br/><br/>
<% }); %>
</ul>
</div>
</body>
</html>
ROUTES table in MLab (so you can see we got ALL the routes)