Upload
rody-middelkoop
View
3.780
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Lecture for CRIA-WT about JavaScript on the server. Explains why we prefer Node.js over PHP and shows the architecture, modules and a small demo application on https://github.com/rodmidde/cria-wt-demo/tree/master/NODE/SimpleMVC.
Citation preview
JavaScript on the server
Node.Js
Logical Architecture RIA
Raw data, relational or not
Something QL-like
Classes, Business or Application Logic
Interface to the back-end
Handles events and calls services
Representation of data, interact with user
Subset of data needed for the view
server
browser
Old Fashioned Setup
MySQL
MySQLi PHP
PHP
XHTML
PHP
http
Disadvantages Old Fashioned Setup
Mix of HTML and PHP Heavy HTTP traffic
server
browser
Classical Setup
MySQL
PDO POPO
PHP
HTML5
JavaScript/jQuery
JSON
http
Disadvantages Classical Setup
Still need to learn different languages with a different development model
PHP is on its return
Don’t put your money on PHP
Hacker News Survey (2012)Python (3,054)Ruby (1,723)JavaScript (1,415)C (970)C# (829)PHP (666)Java (551)C++ (529)Haskell (519)Clojure (459)CoffeeScript (362)Objective C (326)Lisp (322)Perl (311)Scala (233)Scheme (190)Other (188)Erlang (162)Lua (145)SQL (101)
http://readwrite.com/2012/06/05/5-ways-to-tell-which-programming-lanugages-are-most-popular
Job Offerings Dice.com (2012)Java 17,599 (+8.96%)XML 10,780 (+11.70%)JavaScript 10,738 (+11.64%)HTML 9,587 (-1.53%)C# 9,293 (+17.04%)C++ 6,439 (+7.55%)AJAX 5,142 (+15.81%)Perl 5,107 (+3.21%)PHP 3,717 (+23%)Python 3,456 (+32.87%)Ruby 2,141 (+39.03%)HTML5 2,035 (+276.85%)Flash 1,261 (+95.2%)Silverlight 865 (-11.91%)COBOL 656 (-10.75%)Assembler 209 (-1.42%)PowerBuilder 126 (-18.71%)FORTRAN 45 (-33.82%)
Programming Book sales (2012)1. Java2. JavaScript3. C#4. Objective C5. C++6. PHP7. VBA8. Python9. SQL10. ActionScript
server
browser
Modern Setup
MongoDB
Mongoose JSON
JavaScript
HTML5
JavaScript/jQuery
JSON
http
websockets
Advantages Modern Setup
Only one development language: JavaScript
Not only HTTP, WebSockets to the rescue
Disadvantages Modern Setup
OO in JavaScript is doable but PHP syntax resembles Java/C# more
Node hosting is harder to get than PHP hosting
Node’s learning curve is more sheer compared to PHP
Node
Node.js in one slide
Built On Chrome's JavaScript V8 Engine Node.js is a general-purpose JavaScript
runtime with a host of powerful libraries -- one of which happens to provide an HTTP/HTTPS server implementation
Node.js Is Object-Oriented Evented (Async) I/O Package Management with npm
Node Modules
Connect: Extensible HTTP server framework
Socket.IO: WebSockets and Realtime Mocha: BDD/TDD Test Runner Express: Web Framework build on Connect JSLint, JSHint: JavaScript Quality Tools Jasmine: BDD/TDD Test Runner Mongoose: ODM for MongoDB
Node: Connect
var connect = require("connect”);connect().
use(connect.static(__dirname +
"/../client")).listen(8000);
Node: Socket.IO
var io = require("socket.io").listen(1337);io.sockets.on("connection", function (socket) {
socket.on("saveNewPlayer", function (data){
console.log(data);socket.emit("saveReady");
}); });
Node: Socket.IO
Browser initiates
Serverinitiates
WebSockets vs HTTP
Use case A: 1,000 clients
Use case C: 100,000 clients
Use case B: 10,000 clients
http://www.websocket.org/quantum.html
StockQuote using XmlHttpRequest
Browser initiates
StockQuote using WebSockets?
Serverinitiates
Too bad Yahoo does not support WebSockets yet
Node: Write your own module
var Player = require('../model/Player.js');
function EntityManager() {this.saveNewPlayer = function(name,
club, playerNumber, saveReadyFunction) {// do something cool here
}}
module.exports = EntityManager;
Use one or more other modules, external or your own
Simple constructor
Let other modules know what we can do for them
Mongo
Mongo in one slide
Stores data as bson (binary JSON) Console uses JavaScript and json 3rd party GUI tools Database runs as an exe or Windows
service Memory mapped so really needs 64bit OS User group on Google Groups [busy] Sharding for fast access and huge storage
files.meetup.com/2313351/Mongo.pptx
Mongo Architecture
Mongoose
Mongoose in one slide
“Mongoose is the 10gen-supported ODM for Node.js. It has a thriving open source community and includes advanced schema-based features such as async validation, casting, object life-cycle management, pseudo-joins, and rich query builder support.”
Mongoose example
http://mongoosejs.com/index.html