Last active
July 7, 2017 22:41
-
-
Save pllearns/43d5250643e5d097cc48c89f3a491e27 to your computer and use it in GitHub Desktop.
Learn You Node exercises
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 http = require('http') | |
http.get(process.argv[2], (res) => { | |
res.setEncoding('utf8') | |
res.on('data', console.log) | |
res.on('error', console.error) | |
}).on('error', (e) => { | |
console.error(`Got error: ${e.message}`) | |
}) |
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 http = require('http') | |
var bl = require('bl') | |
http.get(process.argv[2], (res) => { | |
res.setEncoding('utf8') | |
res.pipe(bl( (err, data) => { | |
if (err) { | |
console.error(err) | |
} else { | |
data = data.toString() | |
console.log(data.length) | |
console.log(data) | |
} | |
})) | |
}) |
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 http = require('http') | |
var fs = require('fs') | |
var server = http.createServer(function(request, response) { | |
response.writeHead(200, 'application-type/text') | |
fs.createReadStream(process.argv[3]).pipe(response) | |
}) | |
server.listen(Number(process.argv[2])) |
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 http = require('http') | |
var map = require('through2-map') | |
var server = http.createServer(function(request, response) { | |
if(request.method !== 'POST') { | |
response.end('POST \n') | |
} | |
request.pipe(map(function(chunk) { | |
return chunk.toString().toUpperCase() | |
})).pipe(response) | |
}) | |
server.listen(Number(process.argv[2])) |
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
'use strict' | |
var http = require('http') | |
var bl = require('bl') | |
var results = [] | |
var count = 0 | |
function printResults() { | |
for(var i = 0; i < 3; i++) { | |
console.log(results[i]) | |
} | |
} | |
function getUrl(index) { | |
http.get(process.argv[2 + index], function(response) { | |
response.pipe(bl(function(err, data) { | |
if (err) { | |
console.error(err) | |
} | |
results[index] = data.toString() | |
count++ | |
if (count === 3) { | |
printResults() | |
} | |
})) | |
}) | |
} | |
for (var i = 0; i < 3; i++) { | |
getUrl(i) | |
} |
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
// From a discussion on the node github, I wanted to compare the solutions for optimization | |
var http = require('http'); | |
var urls = [process.argv[2], process.argv[3], process.argv[4]]; | |
var holder = []; | |
var count = 1; | |
var endCount = 0; | |
for(var i in urls){ | |
responseCount(i, urls[i]) | |
} | |
function responseCount(count, url){ | |
http.get(url, function(res){ | |
res.setEncoding('utf8'); | |
var string = ''; | |
res.on('data', function(chunk){ | |
string += chunk; | |
}); | |
res.on('end', function(){ | |
holder[count] = string; | |
endCount ++; | |
if(endCount >= 3){ | |
for(var i in holder){ | |
console.log(holder[i]); | |
} | |
} | |
}) | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
} |
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 net = require('net') | |
function fillTime (i) { | |
return (i < 10 ? '0' : '') + i | |
} | |
function dateNow() { | |
var data = new Date() | |
return data.getFullYear() + '-' + | |
fillTime(data.getMonth() + 1) + '-' + | |
fillTime(data.getDate()) + ' ' + | |
fillTime(data.getHours()) + ':' + | |
fillTime(data.getMinutes()) | |
} | |
var server = net.createServer(function (socket) { | |
socket.end(dateNow() + '\n') | |
}) | |
server.listen(Number(process.argv[2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment