Let's rewrite our cities routes using a Route Instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var server = io.connect('http://localhost:8080'); | |
server.on('question', function(question) { | |
insertQuestion(question); | |
}); | |
server.on('answer', function(question, answer) { | |
answerQuestion(question, answer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The following code has a Dynamic Route that takes a year | |
as an argument and returns the city created in that year. | |
The problem with our current implementation is that it | |
breaks when invalid data is sent on client requests. | |
Let's add some basic validation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var server = require('http').createServer(app); | |
var io = require('socket.io').listen(server); | |
var redis = require('redis'); | |
var redisClient = redis.createClient(); | |
io.sockets.on('connection', function(client) { | |
redisClient.lrange("questions", 0, -1, function(err, questions) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clients can also answer each other's questions, | |
so let's build that feature by first listening for | |
the 'answer' event on the client, which will send us | |
both the question and answer, which we want to broadcast | |
out to the rest of the connected clients. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = require('url'); | |
var request = require('request'); | |
var express = require('express'); | |
var options = { | |
protocol: "http:", | |
host: "search.twitter.com", | |
pathname: '/search.json', | |
query: { | |
q: "codeschool" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = process.argv[2]; | |
fs.readFile(path, 'utf8', function(err,data) { | |
var lines = data.split('\n'); | |
console.log(lines.length-1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var path = require('path'); | |
var mydir = process.argv[2]; | |
var ext1 = '.' + process.argv[3]; | |
fs.readdir(mydir, function(err, files){ | |
if(err){ | |
throw err; | |
} | |
//console.log(files); | |
files.forEach(function(filename){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var thefile = process.argv[2]; | |
var file = fs.readFileSync(thefile); | |
var contents = file.toString(); | |
console.log(contents.split('\n').length - 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function plus(x,y) { return x+y }; | |
console.log(process.argv.slice(2).map(Number).reduce(plus)); |
NewerOlder