Skip to content

Instantly share code, notes, and snippets.

@EndangeredMassa
Last active December 14, 2015 00:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EndangeredMassa/5001825 to your computer and use it in GitHub Desktop.
Save EndangeredMassa/5001825 to your computer and use it in GitHub Desktop.
Chicago Node.js: Intro to Node.js Workshop
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.
//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);
});
// logger.js
module.exports = function Logger(logMethod){
this.log = logMethod || console.log;
};
// 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;
//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/");
//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!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment