On Github rajanand02 / introduction-to-nodejs-mongodb-meteor
Raj Anand - Activist / Web Developer
Where does your JavaScript code runs?
Typically in a Browser (client)
- Ryan Dahl
Chrome V8 Engine
Node.js
It is a run time environment
Built on Chrome’s V8 engine
Building network applications on the server-side using JavaScript.
It uses event-driven and non-blocking model.
A Web Framework
Multi-threaded
Beginners
Web socket Server
Streaming server
JSON APIs
file = File.open('introduction.txt', 'r') puts file.read puts "Welcome to Summer Camp 2015"file_read.js
var fs = require('fs'); fs.readFile('introduction.txt' ,'utf8', function ( err, content ){ console.log(content); }); console.log("Welcome to Summer Camp 2015");
var http = require('http'); var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }); server.listen(3000); console.log("Server running at http://127.0.0.1:3000/");
Relational Databases
In 90's
Schema-less
Flexible
Very very fast
Highly Scalable
Open-source
Key-Value based (Redis, Riak)
Document based (MongoDB, CouchDB)
Column-based (Hbase, Cassandra)
Graph-based (Neo4J, OrientDB)
Scalable, High performance, Open source, Document-oriented database.
{ "_id": ObjectId("555cb7798440b2de2c4eef0e"), "user": "xyz", "content": "MongoDB is awesome", "comments": [ { "user": "abc", "comment": "Yes, it is" }, { "user": "efg", "comment": "True that." }, ] }
# Create > db.post.insert( { content: "MongoDB is awesome"} ) > db.post.insert( { content: "Node.js is really cool"} )
# Read > db.post.find() > db.post.findOne( { content: "MongoDB is awesome"} )
# Update > db.post.update( { content: "MongoDB is awesome" }, { $set: { content: "MongoDB is great" } } ) > db.post.update( { content: "MongoDB is great" }, { $set: { content: "MongoDB is awesome" }, { likes: 10 } } )
# Delete > db.post.remove( { likes: 10 }) > db.post.remove({ })
Packaged into one
powerful platform to build modern apps
Focus on your app’s unique features instead of wrangling network code
$ curl https://install.meteor.com | /bin/sh
$ meteor create myapp
$ cd myapp $ meteor => Meteor server running on: http://localhost:3000/
<head> <title>myapp</title> </head> <body> <h1>Welcome to Meteor!</h1> {{> hello}} </body> <template name="hello"> <button>Click Me</button> <p>You've pressed the button {{counter}} times.</p> </template>
if (Meteor.isClient) { // counter starts at 0 Session.setDefault("counter", 0); Template.hello.helpers({ counter: function () { return Session.get("counter"); } }); Template.hello.events({ 'click button': function () { // increment the counter when button is clicked Session.set("counter", Session.get("counter") + 1); } }); } If (Meteor.isServer){ // Server side code }
/client/...
/server/...
/lib/...
/public/...
main.*
Posts = new Meteor.Collection("posts");
Posts.find(); Posts.findOne(); Posts.insert(); Posts.update(); Posts.remove(); ...
// server: publish the messages collection Meteor.publish("messages", function () { return Messages.find(); });
// client: subscribe to the published messages Meteor.subscribe("messages");
Meteor.methods({ addMessage: function (message, room, username) { Messages.insert( { room: room, message: message, username: username } ); } });
// async call Meteor.call('addMessage', message, room, username, function (err, res) { ... } ); // sync call var result = Meteor.call('addMessage', message, room, username);
$ meteor add accounts-ui $ meteor add accounts-*
* = password, facebook, twitter, google, github, ... OAuth2
{{> loginButtons}}
$ meteor add-platform *
$ meteor run *
* = android, ios ...
$ meteor deploy myapp.meteor.com
$ meteor bundle myapp.tgz