• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Node.js Introduction

Node.js Introduction



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.

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.



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

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

    Node.js Introduction Node.js Introduction Presentation Transcript

    • Node.jsJavascript outside the browser
    • 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?
    • 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
    • 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());
    • 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.
    • 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);
    • Socket.ioSocket.io 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 databaseSocket.io is lightweight and responsive, forreal-time server-client communication
    • Socket.io ExampleServervar io = require(socket.io).listen(8080);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>
    • 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
    • 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}
    • 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!
    • Questions?