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

gfnork node.js workshop Lesson #3 node.js basics

gfnork
June 20, 2014

gfnork node.js workshop Lesson #3 node.js basics

first steps, modules, npm, Events

gfnork

June 20, 2014
Tweet

More Decks by gfnork

Other Decks in Programming

Transcript

  1. 2

  2. 3

  3. 4

  4. 5

  5. 6

  6. 7

  7. 10 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200,

    { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');
  8. 11 var http = require('http'); http.createServer(function (req, res) { res.writeHead(200,

    { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');
  9. 12

  10. 13 exports.helloworld = function () { console.log('Hello World'); } var

    test = require('./test.js'); test.helloworld();
  11. 14 module.exports = function () { this.name = "test object";

    this.color = "red"; this.size = "large"; } var test = require('./test.js'); var testObject = new test(); console.log('name:' + testObject.name); console.log('color:' + testObject.color); console.log('size:' + testObject.size);
  12. 15

  13. 16

  14. 17 { "name": "TestNodejsApp", "version": "0.0.0", "description": "TestNodejsApp", "private": true,

    "main": "app.js", "author": { "name": "Qiong Wu", "email": "" }, "dependencies": { "express": "3.4.4", "jade": "*", "stylus": "*" } }
  15. 18

  16. 19

  17. 21 function Test(colour) { this.colour = colour; events.EventEmitter.call(this); this.sendEvent =

    function() { this.emit('EventSent'); } } Test.prototype.__proto__ = events.EventEmitter.prototype; var testObject = new Test('white'); testObject.on('EventSent', function() { console.log('Event received'); }); testObject.sendEvent();
  18. 22

  19. 23 // write 'hello, ' and then end with 'world!'

    http.createServer(function (req, res) { res.write('hello, '); res.end('world!'); // writing more now is not allowed! });
  20. 24