full-stack javascript with node.js
TRANSCRIPT
Full-Stack JavaScriptFull-Stack JavaScriptAn introduction to Node.jsAn introduction to Node.js
When we speak about When we speak about JavaScript …JavaScript …
JavaScript is JavaScript is StrangeStrange
console.log(1 == true); // trueconsole.log(1 === true); // falseconsole.log("0" == false); // trueconsole.log("abc" == "a" + "b" + "c"); // trueconsole.log(null == undefined); // trueconsole.log(30 - "7"); // 23console.log("30" + 7); // 307
JavaScript is even more JavaScript is even more StrangeStrange
function rectangle(x,y) { this.x = x; this.y = y;}
rectangle.prototype.area = function() {return this.x * this.y;
}
var twoByThree = new rectangle(2,3);console.log(twoByThree.area());
But… JavaScript is But… JavaScript is relevant!relevant!
What is Node.jsWhat is Node.js
““Node.js is a platform built on Chrome's JavaScript Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time efficient, perfect for data-intensive real-time applications that run across distributed devices.”applications that run across distributed devices.”
- from nodejs.org
Node.js event loopNode.js event loop
Non-blocking I/ONon-blocking I/OI/O block execution until finished
I/O doesn’t block execution
var data = $.post('/resource.json');console.log(data);
$.post('/resource.json', function (data) { console.log(data);});// script execution continues
Now we have an Now we have an incredibly fast platformincredibly fast platform
Overview of the layers thatOverview of the layers thatmake up a Node applicationmake up a Node application
V8 thread pool(libeio)
event loop(libev)
crypto(OpenSSL)
DNS(c-ares)
Node corequerystring, http, socket, file system,
low-level HTTP parser, low-level TCP server
Community modulesnode-formidable, node-cgi, mongoose, socket.io, express, connect, database drivers, middleware,
routing, etc…
Application logichttp.createServer(), app.use(), route handlers,
directory structures, business algorithms
A simple file serverA simple file server
var http = require("http");
var server = http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n')});
server.listen(3000);
console.log('Server running at http://localhost:3000/');
Node Package Manager Node Package Manager (NPM)(NPM)
• www.npmjs.org
• 60.000+ modules
• package.json file (npm init)
• npm install <module.name> --save
Popular Node.js modulesPopular Node.js modules Express - Express.js, a simple web development
framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
connect - Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance middleware components.
socket.io - Server-side component of the two most common websockets components out there today.
Local modulesLocal modules
exports.world = function () {console.log("Hello World");
};
var hello = require("./hello");
hello.world();
Node.js is Node.js is notnot a silver-bullet new platform that will a silver-bullet new platform that will dominate the web development world. Instead, it’s a dominate the web development world. Instead, it’s a platform that fills a particular need.platform that fills a particular need.
When to NOT use When to NOT use Node.jsNode.js
• When you are doing heavy and CPU intensive calculations on server side, because event-loops are CPU hungry.
• Node.js with a relational DB behind. Relational DB tools for Node.js are still in their early stages.
When to use Node.jsWhen to use Node.js• Node.js is good for creating streaming based real-
time services, web chat applications, static file servers etc.
• If you need high level concurrency and not worried about CPU-cycles.
• Node.js with Express.js can also be used to create classic web applications on the server-side.
Suddenly, every startup on Earth could reuse Suddenly, every startup on Earth could reuse developers (i.e., resources) on both the client- and developers (i.e., resources) on both the client- and server-side, solving the "Python Guru Needed" job server-side, solving the "Python Guru Needed" job post problem.post problem.
Full-Stack JavaScriptFull-Stack JavaScript
Angular.js
Node.js
GruntMochaJasmine.js
DemoDemo
ResourcesResources• Source Code
https://github.com/lehmamic/fullstack-javascript
• Node.js in Action