What is Node JS?
 A JavaScript runtime environment running Google







Chrome’s V8 engine
Node.js is a platform for...
V8 JavaScript Engine

 V8 is written in C++ and is used in Google Chrome
 Runs on Windows (XP or newer), Mac OS X (10.5 ...
What can NodeJS be used for?
 Network servers (HTTP, messaging)
 API backends
 Real time applications

 Streaming data...
Typical architecture
Node Package Manager (NPM)
 NPM is a package management and distribution





system for Node
you can quickly find pa...
Node Package Manager (NPM)
 Databases: mysql, postgresql, mongodb
 DOM: jsdom
 Web frameworks: express, connect

 DOM ...
Non-blocking I/O
 Servers do nothing but I/O (Scripts waiting on I/O

requests degrades performance )
 To avoid blocking...
Blocking vs Non-Blocking flow
Basic HTTP Server
CODE:

http = require("http");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type...
Basic HTTP Server
 This line requires the http module that ships with Node.js and

makes it accessible through the variab...
http.ServerRequest Object
 When listening for request events, the callback gets an

http.ServerRequest object as the firs...
http.ServerResponse Object
 Writing headers: res.writeHead(status, headers)
 Changing or setting a header res.setHeader(...
Basic node JS Module








var mysql
= require('mysql');
var connection = mysql.createConnection({
host : 'localh...
Example
 Free Spark implementation:

http://10.182.39.2/
Cosmin Mereuta
Scalable network applications, event-driven - Node JS
Upcoming SlideShare
Loading in...5
×

Scalable network applications, event-driven - Node JS

393

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
393
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Scalable network applications, event-driven - Node JS

  1. 1. What is Node JS?  A JavaScript runtime environment running Google     Chrome’s V8 engine Node.js is a platform for building fast, scalable network applications (web servers). Server - side JavaScript Uses event-driven, asynchronous I/O to minimize overhead and maximize scalability —Great for data-intensive, real-time applications
  2. 2. V8 JavaScript Engine  V8 is written in C++ and is used in Google Chrome  Runs on Windows (XP or newer), Mac OS X (10.5 or newer), and Linux systems that use IA-32, x64, or ARM – you can install your server on many devices.
  3. 3. What can NodeJS be used for?  Network servers (HTTP, messaging)  API backends  Real time applications  Streaming data  Web sites  Bots
  4. 4. Typical architecture
  5. 5. Node Package Manager (NPM)  NPM is a package management and distribution     system for Node you can quickly find packages to serve specific purposes An npm package is a directory structure with a package.json file describing the package Total Packages: over 31 000 can be installed simply by typing the following: npm install moduleName
  6. 6. Node Package Manager (NPM)  Databases: mysql, postgresql, mongodb  DOM: jsdom  Web frameworks: express, connect  DOM Manipulation: jQuery  Utility libraries: underscore ,backbone, request  Templating: mustache, jade  Testing: vows, expresso
  7. 7. Non-blocking I/O  Servers do nothing but I/O (Scripts waiting on I/O requests degrades performance )  To avoid blocking, Node makes use of the event driven nature of JS by attaching callbacks to I/O requests THE NODE WAY fs.read('very-big-file', function(data) { complexOperation(data); } ); // you can do a lot when waiting for data doOtherStuff();
  8. 8. Blocking vs Non-Blocking flow
  9. 9. Basic HTTP Server CODE: http = require("http"); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.write(“Start Hello"); res.end('Hello Worldn'); })  when ‘stuff’ happens my function fires  I get a request object and a response object  I write to the response object header HTTP status 200 (Standard response for successful HTTP requests) and content-type ‘text/plain’  We make changes to the response object and then these are transmitted when we call: esponse.end();
  10. 10. Basic HTTP Server  This line requires the http module that ships with Node.js and makes it accessible through the variable http.  var http = require("http"); server = http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World"); response.end(); }).listen(8888,”127.0.0.1”);  We pass a function into the server which it uses to handle all incoming requests.  When ‘stuff’ happens call this anonymous function  Listen on port 8888 of the ip 127.0.0.1
  11. 11. http.ServerRequest Object  When listening for request events, the callback gets an http.ServerRequest object as the first argument. This object has methods and properties:  req.url: This property contains the requested URL as a string. It does not contain the schema, hostname, or port, but it contains everything after that.  req.method: This contains the HTTP method used on the request. It can be, for example, GET, POST, DELETE, or HEAD.  req.headers: This contains an object with a property for every HTTP header on the request.
  12. 12. http.ServerResponse Object  Writing headers: res.writeHead(status, headers)  Changing or setting a header res.setHeader(name, value);  Writing a Piece of the Response Body: res.write('Hello');  or use an existing buffer: var buffer = new Buffer('Hello World'); res.write(buffer);  Streaming a mp4 var fs = require('fs'); require('http').createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'video/mp4'}); var rs = fs.createReadStream('test.mp4'); rs.pipe(res); }).listen(4000);
  13. 13. Basic node JS Module        var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'nod', password : 'nodejs', database : "test" });     function connectDatabase() { connection.connect(); console.log("db connected"); }          //insert data to database and call the function received function insertDatabase( data , callback ) {  exports.startDatabase = connectDatabase;  exports.insertDatabase = insertDatabase;; connection.query('INSERT INTO users (user) VALUES (?)',[data.clientName], function ( err, result ) { if (err) throw err; //calling callback function from server-chat.js callback ( result.insertId ); }); }
  14. 14. Example  Free Spark implementation: http://10.182.39.2/
  15. 15. Cosmin Mereuta
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×