Scalable network applications, event-driven - Node JS
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Scalable network applications, event-driven - Node JS

on

  • 481 views

 

Statistics

Views

Total Views
481
Views on SlideShare
481
Embed Views
0

Actions

Likes
1
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scalable network applications, event-driven - Node JS Presentation Transcript

  • 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. 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. What can NodeJS be used for?  Network servers (HTTP, messaging)  API backends  Real time applications  Streaming data  Web sites  Bots
  • 4. Typical architecture
  • 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. 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. 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. Blocking vs Non-Blocking flow
  • 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. 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. 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. 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. 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. Example  Free Spark implementation: http://10.182.39.2/
  • 15. Cosmin Mereuta