Skip to content

Instantly share code, notes, and snippets.

@deanrad
Last active December 27, 2015 13:29
Show Gist options
  • Save deanrad/7333866 to your computer and use it in GitHub Desktop.
Save deanrad/7333866 to your computer and use it in GitHub Desktop.

Node 2 hour course

Agenda

Micro-challenges :

1. Deferred Execution (Browser or Node)

console.log("Hello from Node")

// defer the execution of this
console.log("Goodbye from Node")

// and let this run right away
console.log("Hangin' with Node")

2. Delegated Execution

function say(word) {
  console.log(word);
}

// write a function execute which enables the following
execute(say, "Hello");

3. Input/Output Node-Style

var fs = require('fs');

// turn this pseudo-code into real code
//
// contents = fs.readFile('somefile.txt') 
// console.log("Look at all my money: " + contents);
//
// Hint: lookup the docs for fs.readFile and compare fs.readFileSync

4. Mini Web Server

var http = require("http");

function onRequest(request, response) {
  console.log("Request received.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}

http.createServer(onRequest).listen(8888);

console.log("Server has started.");
// test with browser or
// curl -v http://127.0.0.1:8888/
// ab -n 1000 -c 200 http://127.0.0.1:8888/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment