Node.jsThats what happens when JavaScript runs on Server tooBy Swapnil Mishra
But Javascript on server ?
• Powerful JavaScript engine which powers Google Chrome browserand Chrome OS.• Can also be considered as a Virtual Machine...
• There are differences in the JavaScript which runs in browser vsnode.js.e.g. There are no "window" or "document" objects...
• Model is evented rather than threaded.• Using an event loop, you ask it to do something and it will get back toyou when ...
Example of Blocking I/Opublic INeedData {public Data getMeSomeData(queryParam){Data toBeReturned = db.query(queryParam);iA...
Think about what we really do on web server.• Grab Files• Call Databases• Pull from caches• Wait on other connectionsIts a...
• Totally and so does web-servers like Apache, IIS.• But threading is not free.• There are context switches and executions...
Use a single thread. Do little pieces of work and do them quickly.But single thread !!!We have been using multithreading f...
Apache vs NGINX
Apache vs NGINX
The difference?• Apache uses one thread per connection.• NGINX does not use threads. It uses an event loop.So basically we...
• Node.js is a set of bindings for writing high-performance networkservers.• Exposes only non-blocking I/O APIs• Stops us ...
• Node.js is being used by many companies including big names likeLinkedin, WalMart in fact Linkedins mobile backend is to...
Thank You !!!Time for a quick demo
Upcoming SlideShare
Loading in …5
×

Node.js primer

1,380 views

Published on

This is the talk i gave in my office to introduce/highlight advantages of node.js.

Published in: Education, Technology
  • Be the first to like this

Node.js primer

  1. 1. Node.jsThats what happens when JavaScript runs on Server tooBy Swapnil Mishra
  2. 2. But Javascript on server ?
  3. 3. • Powerful JavaScript engine which powers Google Chrome browserand Chrome OS.• Can also be considered as a Virtual Machine.• Node.js runs on the top of V8.• Utilizes the networking and parsing features which are done elegantlyin V8.V8 made it possible
  4. 4. • There are differences in the JavaScript which runs in browser vsnode.js.e.g. There are no "window" or "document" objects.• Things which have been taken from V8 are asynchronous/non-blocking nature of JavaScript, Syntax, Data Structures(most of them).Which means everyones favorite closure/anonymous functions willwork here too.• Things which have been added into it are Timers, Processes, Events,Buffers, Streams, Crypto, File Systems, HTTP, HTTPS and many more.Wait, so you are saying this node.js is sameas the javascript which runs in browser ?
  5. 5. • Model is evented rather than threaded.• Using an event loop, you ask it to do something and it will get back toyou when its done.(we will see an example later)• Conventional languages are run in threaded environment where theycreate threads and just loop/pool in to wait for the result. Thats notthe case with node.js.• Because of the evented nature it can perform Non-Blocking NetworkI/O.Oh cool tell me more about it !!
  6. 6. Example of Blocking I/Opublic INeedData {public Data getMeSomeData(queryParam){Data toBeReturned = db.query(queryParam);iAmBlockedTillQueryExecutes();return toBeReturned;Blocking I/O}}// get data performs blocking I/O call because of which// iAmBlockedTillQueryExecutes() is blockedWhy should i care about Non-Blocking I/O?
  7. 7. Think about what we really do on web server.• Grab Files• Call Databases• Pull from caches• Wait on other connectionsIts all I/O and we know that I/O latency ofnetwork is highest.Why should i care about Non-Blocking I/O?
  8. 8. • Totally and so does web-servers like Apache, IIS.• But threading is not free.• There are context switches and executions stacks takememory.What should we do then ?Hey but thats exactly why i use thread
  9. 9. Use a single thread. Do little pieces of work and do them quickly.But single thread !!!We have been using multithreading from ageswhen UI used to be in "Java Swing" !!!Are you crazy ?Can it handle ?Event loop comes to the rescue
  10. 10. Apache vs NGINX
  11. 11. Apache vs NGINX
  12. 12. The difference?• Apache uses one thread per connection.• NGINX does not use threads. It uses an event loop.So basically we have to change our programming model to somethingwhere we could easily write non-blocking code.public INeedData {public Data getMeSomeData(queryParam){Data toBeReturned = db.query(queryParam,function(){returntoBeReturned;});iAmNotBlockedTillQueryExecutes();}}Apache vs NGINX
  13. 13. • Node.js is a set of bindings for writing high-performance networkservers.• Exposes only non-blocking I/O APIs• Stops us writing code that behaves badly.Below is code for a simple http server in Node.jsvar http = require(http);http.createServer(function (req, res) {res.writeHead(200, {Content-Type: text/plain});res.end(Hello Worldn);}).listen(1337, 127.0.0.1);console.log(Server running at http://127.0.0.1:1337/);Now i got it
  14. 14. • Node.js is being used by many companies including big names likeLinkedin, WalMart in fact Linkedins mobile backend is totally onnode.js.• Excellent package manager npm(Node Package Manager).• Installing packages: npm install <package>• npm can also be used for dependency management just like maven.• One more reason for Node.jss popularity is huge base of javascriptdevelopers who can now write backend code too.Production Usage ?
  15. 15. Thank You !!!Time for a quick demo

×