Node.js Code School course notes

Posted by Monik, 02 January 2015.
Programming Node.js
Course notes

These are the notes I took during the Node.js basic course from www.codeschool.com. It was a very good course. It guides through the basics of how to start with NodeJS.

Table of contents

Lesson 1

Minimal server

var http = require(‘http’);

http.createServer(function(request, response) {
   response.writeHead(200);
   response.write(“Hello, this is Bla”);
   response.end()
}).listen(8080);

Serving index.html

http.createServer(function(request, response) {
  response.writeHead(200);
  fs.readFile('index.html', function(error, contents){
    response.write(contents);
    response.end();
  });
}).listen(8080);

Lesson 2: Events

Custom EventEmitter

var events = require('events');
var EventEmitter = events.EventEmitter;

var chat = new EventEmitter();
chat.on('message', function(message){
  console.log(message);
});
chat.emit('message', 'blabla');

Lesson 3: Streams

Echoing the request to the user

response.writeHead(200);
request.pipe(response); // echo the request to the user

File upload

var file = fs.createWriteStream(‘file1.txt’);
request.pipe(file);
request.on(‘end’, function(){
  response.end(‘uploaded!’)
});

File upload with progress

var file = fs.createWriteStream('file1.txt');

var fileBytes = request.headers[content-length];
var uploadedBytes = 0;

request.on('readable', function(){
  var chunk = null;
  while(null !== (chunk = file.read())){
    uploadedBytes += chunk.length;
    var progress = (uploadedBytes/fileBytes)*100;
    response.write("Progress:"+ parseInt(progress,10)+"%\n");
  }

});

request.pipe(file);
request.on('end', function(){
  response.end('uploaded!')
});

Other useful things

Lesson 4: Modules

my_module.js:
var foo = function(){...};
var bar = function(){...};
var baz = function(){...};
exports.foo = foo;
exports.bar = bar;

app.js:
var m = require("./my_module");
m.foo();
m.bar();

Lesson 5: Express

Basic example

require('express');

var app = express();
app.get('/', function(request, response){
  response.sendFile(__dirname + "/index.html");
});
app.listen(8080);

Request with dynamic URI param

app.get('/tweets/:username', function(request, response){
  var username = request.params.username;
  options = {
    protocol: "http:",
    host: "api.twitter.com",
    pathname: "/1/statuses/user_timeline.json",
    query: {screen_name: username, count: 10}
  };
  var twitterUrl = url.format(options);
  request(twitterUrl).pipe(response);
});
app.listen(8080);

Templates - Embedded Java Script

Lesson 6: Sockets

Server side

app.js
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(client){
  console.log("New client connected..");
  //when client sends a message, emit it to all other clients
  client.on('messages', function(data){
    client.broadcast.emit('question',data);
  });
  //or just emit one dummy message to the client
  client.emit('messages',{foo:"bar"});
});
server.listen(8080);

Client side

index.html:
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080');
  //the client can receive messages like this


  server.on('messages', function(data){

    console.log(data.foo);

  });


</script>

Lesson 7: Persisting Data



Comments


Comments: