|
C O U R S E L E C T U R E MongoDB, node.js, Express and SWIG Notes taken on June 9, 2014 by Edward Tanguay |
package manager for node.js
express = require('express') in app.js
so we have do npm install express
also requires express and mongodb
we could install these indivually or with package.json
we just have to say npm install
installs them in node_modules
directory
you could also install them globally
communicates with mongod with BSON, a binary jSON
driver is written in javascript
handles things behind the scenes
cannot find module mongodb
installs mongodb module
var mc = require('mongodb').MongoClient;
pass in a callback, it calls it when done connecting
takes care of routing and details of web application for us
callback takes two arguments
app.get has respond and request in callback
express server started on port 8080
in app.get, the star is wildcard for any handler that is not handled
extending helloworld in express
make file views/hello.html
templating engine: consolidate
a set of wrappers for template libraries for express
app.engine('html',cons.swig);
cons is consolidate and swig is the templating engine
helloworld with mongodb, express, and swig
adding calls to mongo db from the last example
Server = require('mongodb').Server;
told it to use the native BSON parser
not registering a callback
it looks like we are using our database connection before it is even connected
but we actually start listening as soon as the database connection is open
we are just setting up mongoClient as an object and then pass it as a parameter in the callback
express: handling GET requests
registering only one route
express: handling POST requests
form to pick radio button choice
we have app.get and app.post
express.bodyParser() populates req.body so we can access it
error handling function
call next with an error object
in RDBMS if you want to add a new field to a row, you need to create a new column for all rows in the table
you don't need to do this in mongodb
this allows mongodb to be very agile
there are only two data structures inside JSON
{ key : value, key : value }
database model if we used RDBMS
database model in mongodb
you can embed the comments in the posts
if the email is missing from any comment, it doesn't matter, you can leave it out
it is the way you access the data that guides how to design your data structures