Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js Introduction

Node.js Introduction

A brief introduction to Node.js given at the Grand Rapids Web Development Group.

Brandon Keepers

March 26, 2012
Tweet

More Decks by Brandon Keepers

Other Decks in Programming

Transcript

  1. INTRODUCTION

    View full-size slide

  2. Hi, I’m
    @bkeepers

    View full-size slide

  3. nodejs.org
    Node.js is a platform built on Chrome's
    JavaScript runtime for easily building
    fast, scalable network applications.
    Node.js uses an event-driven, non-
    blocking I/O model that makes it
    lightweight and efficient, perfect for
    data-intensive real-time applications
    that run across distributed devices.

    View full-size slide

  4. server side
    JavaScript

    View full-size slide

  5. $ node webserver.js
    var http = require('http'),
    server = http.createServer();
    server.on('request', function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
    });
    server.listen(1337, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:1337/');
    webserver.js

    View full-size slide

  6. event loop
    modules
    package
    management

    View full-size slide

  7. non-blocking
    evented I/O

    View full-size slide

  8. event driven

    View full-size slide

  9. event driven
    Button

    View full-size slide

  10. event driven
    Button

    View full-size slide

  11. event driven
    Button
    $('button').on('click', function(event) {
    alert('Event Driven!')
    });

    View full-size slide

  12. event driven
    server.on('request', function(req, res) {
    res.write(handleRequest(req))
    });

    View full-size slide

  13. non-blocking

    View full-size slide

  14. // blocking
    var files = fs.readdirSync('/tmp')
    for(var i = 0; i < files.length; i++) {
    var file = files[i];
    fs.unlinkSync('/tmp/' + file);
    console.log('successfully deleted ' + file);
    }

    View full-size slide

  15. // blocking
    var files = fs.readdirSync('/tmp')
    for(var i = 0; i < files.length; i++) {
    var file = files[i];
    fs.unlinkSync('/tmp/' + file);
    console.log('successfully deleted ' + file);
    }
    // non-blocking
    fs.readdir('/tmp', function(err, files) {
    for(var i = 0; i < files.length; i++) {
    var file = files[i];
    fs.unlink('/tmp/' + file, function (err) {
    if (err) throw err;
    console.log('successfully deleted ' + file);
    });
    }
    });

    View full-size slide

  16. CommonJS
    modules

    View full-size slide

  17. JavaScript
    Pollutes

    View full-size slide

  18. JavaScript
    Pollutes
    string = "pollution";

    View full-size slide

  19. var http = require('http');

    View full-size slide

  20. hello.js
    module.exports = function() {
    return 'Hello World'
    };

    View full-size slide

  21. $ node myapp.js
    myapp.js
    var hello = require('./hello.js');
    console.log(hello());

    View full-size slide

  22. package
    management

    View full-size slide

  23. $ npm install

    View full-size slide

  24. package.json

    View full-size slide

  25. package.json
    $ npm install
    {
    "name": "myapp",
    "version": "0.0.1",
    "dependencies": {
    "socket.io": "0.8.7",
    "coffee-script": "1.2.0",
    "spine": "~1.0.5"
    }
    }

    View full-size slide

  26. building the simplest
    chat app in the world
    demo

    View full-size slide

  27. http://nodejs.org/api/

    View full-size slide

  28. thanks!
    @bkeepers

    View full-size slide