Mongo M101JS Week 1


Introduction to Schema Design (? min)
Blog in Documents (? min)
Blog in Relational Tables (? min)
Introduction to class project - Blog (? min)
JSON Spec (? min)
JSON subdocuments (? min)
JSON Revisited (? min)
MongoDB is Schemaless (? min)
Express: Handling POST Requests  (? min)
Express: Handling GET Requests (? min)

Hello World using Express, Swig, and Mongo (5.5 min)



Hello World using Express and Swig (3.5 min)


Consolidate is a library handles
Swig is the template engine

Here is the app.js:
var express = require(express),
app = express(),
cons = require(consolidate); // Templating library adapter for Express

app.engine(html, cons.swig);
app.set(view engine, html);
app.set(views, __dirname + /views); // html files stored in directory views

app.get(/, function(req, res){
        res.render(hello, { name : World });
        });

app.get(*, function(req, res){
        res.send(Page Not Found, 404);
        });

app.listen(8080);

console.log(Express server started on port 8080);

hello.html:
<h1>Hello, {{name}}!</h1>

Hello World using Express (4 min)


Express is a library for node.js which handles routing and details in a web application context.

// to install the express module for node.js use the following:
//   npm install express

var express = require(express);
var app = express(); // create instance of application

app.get(/, function(req, res){
    res.send(Hello World);
});

app.get(*, function(req, res){
    res.send(Page Not Found, 404);
});

// start the server
var port = 8082;
app.listen(port);
console.log(Express server started on port +port);


$ node app.js 
Express server started on port 8082

$ curl http://localhost:8082
Hello World

HelloWorld mongodb style (? min)

Introduction to npm (3 min)


npm is the package manager for node.js
require statements used to include external libraries in node.js.
var express=require(express);
var consolidate = require( consolidate );
var mongodb = require( mongodb );
package.json - has metadata about the package and dependencies



Hello world on Node.js (3 min)


Asynchronous vs. Synchronous (4.5 min)


mongo shell is synchronous
node.js is asynchronous


// mongo shell: 
// Find one document in our collection
var doc = db.coll.findOne();

// Print the result

printjson(doc);

// node.js: Find one document in our collection
// file name:  app.js
var MongoClient = require(mongodb).MongoClient;

MongoClient.connect(mongodb://127.0.0.1:27017/test, function(err, db) {

    if(err) throw err;

    // Find one document in our collection
    db.collection(coll).findOne({}, function(err, doc) {

        // Print the result
        console.dir(doc);

        // Close the DB
        db.close();
    });

    // Declare success
    console.dir("Called findOne!");

});

Run the node app:
$ node app.js

Installing Node.js (mac) (1 min)


Installing from nodejs.org
Verify it is installed:
$ node 
> console.log("Hello, Node")
Hello, Node

undefined
Node.js is a platform built on Chromes JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.




Introduction to JSON (? min)
Quick Introductoin to the Mongo Shell (? min)
Installing MongoDB windows (? min)
Installing MongoDB mac (? min)
System Requirements (? min)
Overview of Building an App with MongoDB (? min)
MongoDB relative to Relational (? min)
What is MongoDB? (? min)
Welcome to M101JS (2.5 min)

Related Posts by Categories

0 komentar:

Posting Komentar