public
Last active

Chicago Node.js: Intro to Node.js Workshop

  • Download Gist
0.README
1 2 3 4 5 6 7
Slides: https://docs.google.com/presentation/d/17qCqjo9MgNKdH3i0pVgqkbXyVPYS-7_COCi-etR9new/pub?start=false&loop=false&delayms=3000#slide=id.gab72b792_05
 
Event: http://www.meetup.com/Chicago-Nodejs/events/98566142/
 
To select the code in the slides, go to the gear menu in the bottom-left and select Speaker Notes.
 
Answers are below.
1.Read a File.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//index.js
 
var fs = require('fs');
 
var fileName = process.argv[2];
 
fs.readFile(fileName, function(err, contents){
if(err)
return console.log(err.message);
 
// may vary depending on line endings of your OS
var lines = contents.toString().split('\n').length - 1
console.log(lines);
});
2.Create a Module.js
JavaScript
1 2 3 4 5
// logger.js
 
module.exports = function Logger(logMethod){
this.log = logMethod || console.log;
};
3.Emit some Events.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// radioactive_element.js:
 
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
function Element(symbol, startingSize, decayMs){
this.symbol = symbol;
this.size = startingSize;
 
// preserve the scope so that we can use it inside a nested function, like checkDecay
var self = this;
 
var intervalHandle = setInterval(checkDecay, decayMs);
 
function checkDecay(){
var chance = Math.random();
if (chance > 0.5) {
self.size--;
self.emit('decay', self);
 
if(self.size == 0){
self.emit('dead', self);
//stop checking
clearInterval(intervalHandle);
}
}
}
};
 
//inherit the EventEmitter goodness
inherits(Element, EventEmitter);
module.exports = Element;
4.Make an HTTP Server.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
//index.js
 
var http = require('http');
var viewCount = 0;
 
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
 
if(request.url === '/'){
response.write('I just counted your view!');
viewCount++;
}
 
if(request.url === '/viewcount'){
response.write(viewCount.toString());
}
 
response.end();
});
 
server.listen(8000);
console.log("Server running at http://localhost:8000/");
5.Do Some Routing.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//index.js
 
var http = require('http');
 
var viewCount = 0;
 
var server = http.createServer(function (request, response) {
console.log('Handling: '+request.method+' : '+request.url);
response.writeHead(200, {"Content-Type": "text/plain"});
 
if(request.url === '/count' && request.method === 'POST'){
viewCount++;
}
 
if(request.url === '/count' && request.method === 'DELETE'){
viewCount--;
}
 
if(request.url === '/count' && request.method === 'GET'){
response.write(viewCount.toString());
}
 
// use a regular expression to match a variable route
var countRegex = new RegExp('/count/(\\d+)');
var countMatch = request.url.match(countRegex)
if(request.method === 'PUT' && countMatch){
viewCount = parseInt(countMatch[1], 10);
}
 
// use a regular expression to match a variable route
var yeahRegex = new RegExp('/y(e+)ah');
var yeahMatch = request.url.match(yeahRegex);
if(request.method === 'GET' && yeahMatch){
var message = 'y' + yeahMatch[1] + 'ah!';
response.write(message.toUpperCase());
}
 
response.end();
});
 
server.listen(8000);
console.log("Server running at http://localhost:8000/");
 
 
// We can test the routes with curl, like so:
 
/*
$ curl -X POST http://localhost:8000/count
$ curl -X POST http://localhost:8000/count
$ curl -X GET http://localhost:8000/count
2
$ curl -X DELETE http://localhost:8000/count
$ curl -X GET http://localhost:8000/count
1
$ curl -X PUT http://localhost:8000/count/10
$ curl -X GET http://localhost:8000/count
10
$ curl -X GET http://localhost:8000/yeeeeeeeeeeeeeeah
YEEEEEEEEEEEEEEAH!
$ curl -X GET http://localhost:8000/yeah
YEAH!
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.