CS651 | Web Systems
  • outline
  • projects
  • syllabus
  • links

Exercise N1: NodeJS with Express and MongoDB on Heroku

 Due March 15, start of class, GROUP WORK

 

 Use WebStorm with Git and Heroku and create a NodeJS+Express project. Setup free acount on Heroku and follow all of the steps of getting started and setup a MongoDB database table as shown here (SEE NOTES BELOW) and code and view ejs page to support the url /db as demoed here (note: read the ejs file to see what the fields should be of your database table). Demonstrate the application working in a web browser and show heroku console illustrating the app is deployed and a 3rd database console which shows the data in the database from app working.  Also turn in as directed in deliverables below.

 

 

 

SPECIAL Note: for MongoDB with Heroku

>> p.s. I do know MLab is  shut down since Nov. 2020 so you now need to investigate what are your options (remember you are responsible for any and all accounts you create)

  • SUGGESTED OPTION: Using the credits you get on Github student developer pack for MongoDB Atlas 
  • Another option that is not free is using the new plugin OpenRocket for MongoDB support.   Please use this if it is free. https://devcenter.heroku.com/articles/ormongo
  • ALSO read this article on NodeJS and provisioning a DB on Heroku and various options


IMPORTANT: Since creating some of the screenshots shown above the version of MongoDB and the corresponding drivers have some changes in the code --how you call it from Node JS-without it the code wont work.

HERE is  code --note that also when you go to close the connection the code also alters to use the new client object that it receives

 

var express = require('express');

var mongodb = require('mongodb');

var router = express.Router();

 

/* GET home page. */

router.get('/', function(req, res, next) {

  res.render('index', { title: 'Express' });

});

 

module.exports = router;

 

 

//**************************************************************************

//***** mongodb get all of the Routes in Routes collection where frequence>=1

//      and sort by the name of the route.  Render information in the views/pages/mongodb.ejs

router.get('/mongodb', function (request, response) {

 

    mongodb.MongoClient.connect('mongodb://heroku_pmk6n54s:penh0a964unc8citdi3c1943cv@ds153869.mlab.com:53869/heroku_pmk6n54s', function(err, client) {

    // mongodb.MongoClient.connect(process.env.MONGODB_URI, function(err, db) {  // works with mongodb v2 but not v3

        if(err) throw err;

        //get collection of routes

        var db = client.db('heroku_pmk6n54s');  // in v3 we need to get the db from the client

        var Routes = db.collection('Routes');

        //get all Routes with frequency >=1

        Routes.find({ frequency : { $gte: 0 } }).sort({ name: 1 }).toArray(function (err, docs) {

            if(err) throw err;

 

            response.render('mongodb', {results: docs});

 

        });

 

        //close connection when your app is terminating.

        // db.close(function (err) {

         client.close(function (err) {

            if(err) throw err;

        });

    });//end of connect

});//end app.get

 

 

 

 

Deliverable

(TURN IN EVERYONE IN GROUP : URL to a NEW gitHub responsitory to BB->Exercises->Ex N1.
It should contain ALL the CODE and a wiki with the following page names.

  • App Runnning: screenshots of application running ( url /db )

    DataBase Console: screenshots of data in database admin console

 

cs651:web systems

  • home
  • outline
  • projects
  • syllabus
  • links