Node.js Introduction


Published on

An introduction to Node.JS -- why its cool and useful and powerful. Allen Cook's slides from his presentation to the KYJSUG on Feb 28.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Node.js Introduction

  1. 1. Node.jsJavascript outside the browser
  2. 2. What is Node.js?Node is a way to use Javascript outside thebrowser, on the server side.At its core, its a command line version ofJavascript.node script.jsHow does this let you serve webpages?
  3. 3. The Modular Server● Out of the box, node cant serve webpages● Handling HTTP requests happens with a module (express)● Routing, Authentication and Database layers are all separate modules● Find and install node modules with npm● You wire the modules together with code
  4. 4. Modular Example// to serve a webpagevar express = require(express);// login modulevar passport = require(passport);// provide a callback for the indexvar app = express();app.get("/", function(......// require authenticationapp.use(passport.initialize());
  5. 5. Express, the webserverWhen node.js needs to act as a webserver,typically you use express.Express handles listening on a port for HTTPrequests and responsesIn typical node.js fashion, express has manymodules handling JSON parsing, basicauthentication, sessions and more.
  6. 6. Express "Hello World"var express = require(express);var app = express();app.get(/hello.txt, function(req, res){ var body = Hello World; res.setHeader(Content-Type, text/plain); res.setHeader(Content-Length, body.length); res.end(body);});app.listen(80);
  7. 7. is used to communicate with thebrowsers Javascript app.When using client-side apps, such asBackbone or KnockoutJS, you cancommunicate changes to node.js for saving toa is lightweight and responsive, forreal-time server-client communication
  8. 8. ExampleServervar io = require(;io.sockets.on(connection, function (socket) { socket.on(data:save, function (data) { // save data to database });});Client<script> var socket = io.connect(http://localhost); // save data to servers database socket.emit(data:save, { my: data });</script>
  9. 9. Scaling with ClusterNode.js, like Javascript, only has one thread!For a webserver, this makes every request waiton the previous request to complete beforebeing processed.Fortunately, cluster lets you start the samenode.js script in multiple threads. Similar to Cs"fork" method
  10. 10. Cluster Examplevar cluster = require(cluster);if (cluster.isMaster) { require(os).cpus().forEach( function () { cluster.fork(); });} else { // Serve your app, this code will // run multiple times, once for // each CPU on your server}
  11. 11. Many more modules!● Mongoose to communicate with MongoDB● fs for using the servers filesystem● vm for running new sandboxed Javascripts● node-mysql for MySql● Lots more! Use npm search to find any module you want!
  12. 12. Questions?