full-stack javascript with node.js

22
Full-Stack JavaScript Full-Stack JavaScript An introduction to Node.js An introduction to Node.js

Upload: michael-lehmann

Post on 13-Apr-2017

2.140 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Full-Stack JavaScript with Node.js

Full-Stack JavaScriptFull-Stack JavaScriptAn introduction to Node.jsAn introduction to Node.js

Page 2: Full-Stack JavaScript with Node.js

When we speak about When we speak about JavaScript …JavaScript …

Page 3: Full-Stack JavaScript with Node.js

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

Page 4: Full-Stack JavaScript with Node.js

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());

Page 5: Full-Stack JavaScript with Node.js

But… JavaScript is But… JavaScript is relevant!relevant!

Page 6: Full-Stack JavaScript with Node.js

What is Node.jsWhat is Node.js

Page 7: Full-Stack JavaScript with 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

Page 8: Full-Stack JavaScript with Node.js

Node.js event loopNode.js event loop

Page 9: Full-Stack JavaScript with Node.js

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

Page 10: Full-Stack JavaScript with Node.js

Now we have an Now we have an incredibly fast platformincredibly fast platform

Page 11: Full-Stack JavaScript with Node.js

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

Page 12: Full-Stack JavaScript with Node.js

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/');

Page 13: Full-Stack JavaScript with Node.js

Node Package Manager Node Package Manager (NPM)(NPM)

• www.npmjs.org

• 60.000+ modules

• package.json file (npm init)

• npm install <module.name> --save

Page 14: Full-Stack JavaScript with Node.js

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.

Page 15: Full-Stack JavaScript with Node.js

Local modulesLocal modules

exports.world = function () {console.log("Hello World");

};

var hello = require("./hello");

hello.world();

Page 16: Full-Stack JavaScript with Node.js

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.

Page 17: Full-Stack JavaScript with Node.js

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.

Page 18: Full-Stack JavaScript with Node.js

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.

Page 19: Full-Stack JavaScript with Node.js

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.

Page 20: Full-Stack JavaScript with Node.js

Full-Stack JavaScriptFull-Stack JavaScript

Angular.js

Node.js

GruntMochaJasmine.js

Page 21: Full-Stack JavaScript with Node.js

DemoDemo

Page 22: Full-Stack JavaScript with Node.js

ResourcesResources• Source Code

https://github.com/lehmamic/fullstack-javascript

• Node.js in Action