Last active
September 23, 2018 21:39
-
-
Save Anna-Myzukina/8a6a53123033f15fbf0bcf5bb655caed to your computer and use it in GitHub Desktop.
learnyounode
This file contains hidden or 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
//Exercise 1 | |
console.log('HELLO WORLD'); |
This file contains hidden or 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
//Exercise 10 | |
var net = require('net'); | |
var port = parseInt(process.argv[2]); | |
function thistime(){ | |
var date = new Date(Date.now()); | |
var hours = "0" + date.getHours(); | |
var minutes = "0" + date.getMinutes(); | |
var year = date.getFullYear(); | |
var day = "0" + date.getDate(); | |
var month = "0" + (date.getMonth() + 1); | |
return year + "-" + month.substr(-2) + "-" + day.substr(-2) + " " + hours.substr(-2) + ':' + minutes.substr(-2) + "\n"; | |
} | |
var server = net.createServer( function (socket) { | |
socket.write(thistime()); | |
socket.end(); | |
}); | |
server.listen(port); |
This file contains hidden or 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
//Exercise 11 | |
var http = require('http'); | |
var fs = require('fs'); | |
var port = process.argv[2]; | |
var filepath = process.argv[3]; | |
var server = http.createServer( function (req,res){ | |
var src = fs.createReadStream(filepath) | |
src.pipe(res); | |
}) | |
server.listen(port); |
This file contains hidden or 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
//Exercise 12 | |
var http = require('http'); | |
var map = require("through2-map"); | |
var server = http.createServer( function callback (req,res) { | |
if(req.method == "POST"){ | |
req.pipe(map(function (chunk){ | |
return chunk.toString().toUpperCase(); | |
})).pipe(res); | |
} | |
}); | |
server.listen(process.argv[2]); |
This file contains hidden or 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
//Exercise 13 | |
var http = require('http'); | |
var url = require('url'); | |
var portNumber = process.argv[2]; | |
http.createServer(function (req, res) { | |
var urlObject = url.parse(req.url, true), | |
pathname = urlObject.pathname, | |
startTime = urlObject.query.iso, | |
result; | |
if (pathname === '/api/unixtime') { | |
result = getUnixTimeStamp(startTime); | |
} else if (pathname === '/api/parsetime') { | |
result = getTimeObj(startTime); | |
} | |
if (result) { | |
res.writeHead(200, { | |
'Content-type': 'application/json' | |
}); | |
res.end(JSON.stringify(result)); | |
} else { | |
res.writeHead(404); | |
res.end(); | |
} | |
}).listen(portNumber) | |
function getUnixTimeStamp(startTime) { | |
return { | |
unixtime: getTimeStamp(startTime) | |
}; | |
} | |
function getTimeStamp(startTime) { | |
return Date.parse(startTime); | |
} | |
function getTimeObj(startTime) { | |
var date = new Date(getTimeStamp(startTime)); | |
return { | |
hour: date.getHours(), | |
minute: date.getMinutes(), | |
second: date.getSeconds() | |
}; | |
} | |
/*var http = require('http'); | |
var url = require('url'); | |
function parsetime (time) { | |
return { | |
hour: time.getHours(), | |
minute: time.getMinutes(), | |
second: time.getSeconds() | |
} | |
} | |
function unixtime (time) { | |
return { | |
unixtime : time.getTime() | |
} | |
} | |
var server = http.createServer(function (req, res) { | |
var parsedUrl = url.parse(req.url, true); | |
var time = new Date(parsedUrl.query.iso); | |
var result | |
if (/^\/api\/parsetime/.test(req.url)){ | |
result = parsetime(time); | |
}else if (/^\/api\/unixtime/.test(req.url)){ | |
result = unixtime(time) | |
} | |
if (result) { | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
res.end(JSON.stringify(result)); | |
} else { | |
res.writeHead(404); | |
res.end(); | |
} | |
}) | |
server.listen(Number(process.argv[2]));*/ |
This file contains hidden or 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
//Exercise 2 | |
var result = 0 | |
for (var i = 2; i < process.argv.length; i++) { | |
result += num(process.argv[i]) | |
} | |
console.log(result); |
This file contains hidden or 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
//Exercise 3 | |
var fs = require('fs'); | |
var filePath = process.argv[2]; | |
var output = fs.readFileSync(filePath); | |
output = output.toString().split('\n'); | |
console.log(output.length - 1); | |
/*var fs = require('fs'); | |
var contents = fs.readFileSync(process.argv[2]); | |
var lines = contents.toString().split('\n').length - 1; | |
console.log(lines);*/ |
This file contains hidden or 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
//Exercise 4 | |
var fs = require('fs'); | |
var file = process.argv[2]; | |
output = fs.readFile(file, function callback(err, data) { | |
if (err) { | |
return console.log(err); | |
} | |
console.log(data.toString().split('\n').length - 1); | |
}); |
This file contains hidden or 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
//Exercise 5 | |
var fs = require('fs'); | |
var path = require('path'); | |
var ext = process.argv[3]; | |
var dir = process.argv[2]; | |
fs.readdir(dir, function callback(err, list){ | |
if(err){ | |
console.log(err); | |
} | |
for(var i = 0; i < list.length; i++){ | |
var file = path.extname(list[i]); | |
if(file == "."+ext){ | |
console.log(list[i]); | |
} | |
} | |
}) | |
/* | |
var fs = require('fs') | |
var path = require('path') | |
var folder = process.argv[2] | |
var ext = '.' + process.argv[3] | |
fs.readdir(folder, function (err, files) { | |
if (err) return console.error(err) | |
files.forEach(function (file) { | |
if (path.extname(file) === ext) { | |
console.log(file) | |
} | |
}) | |
}) | |
*/ |
This file contains hidden or 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
//Exercise 6-01 | |
var fs = require('fs'); | |
var path = require('path'); | |
module.exports = function (dir, fileString, callback) { | |
fs.readdir(dir, function (err, list) { | |
if (err){ | |
return callback(err); | |
} | |
list = list.filter(function (file) { | |
return path.extname(file) === '.' + fileString; | |
}) | |
callback(null, list); | |
}) | |
} |
This file contains hidden or 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
//Exercise 6-02 | |
var filterFn = require('./modFirst.js'); | |
var dir = process.argv[2]; | |
var filterStr = process.argv[3]; | |
filterFn(dir, filterStr, function (err, list) { | |
if (err){ | |
return console.error( err); | |
} | |
list.forEach(function (file) { | |
console.log(file); | |
}) | |
}) |
This file contains hidden or 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
//Exercise 7 | |
var http = require('http'); | |
var urlPath = process.argv[2]; | |
http.get(urlPath, function callback(response) { | |
response.setEncoding('utf8'); | |
response.on('data', function(data){ | |
console.log(data); | |
}) | |
}); | |
/*var http = require('http'); | |
var url = process.argv[2].toString(); | |
http.get(url, function (response) { | |
response.setEncoding('utf8'); | |
response.on('data', console.log); | |
response.on('error', console.error); | |
}).on('error', console.error);*/ |
This file contains hidden or 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
//Exercise 8 | |
var http = require('http'); | |
var urlPath = process.argv[2]; | |
var allData= ""; | |
http.get(urlPath, function callback (response){ | |
response.setEncoding("utf8"); | |
response.on("data", function(data){ | |
allData = allData + data; | |
}) | |
response.on("end", function(){ | |
console.log(allData.length); | |
console.log(allData); | |
}) | |
}); | |
/*var http = require('http'); | |
var bl = require('bl'); | |
http.get(process.argv[2], function (response) { | |
response.pipe(bl(function (err, data) { | |
if (err){ | |
return console.error(err); | |
} | |
data = data.toString(); | |
console.log(data.length); | |
console.log(data); | |
})) | |
})*/ |
This file contains hidden or 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
//Exercise 9 | |
var http = require('http'); | |
var bl = require('bl'); | |
var urls = []; | |
var completed = 0; | |
var all = []; | |
for(var i=2; i < process.argv.length; i++){ | |
urls.push(process.argv[i]); | |
} | |
for(i in urls){ | |
getdata(i); | |
} | |
function getdata(count){ | |
http.get(urls[count],function(response){ | |
response.pipe(bl(function(err, data){ | |
if(err) { | |
return console.error(err); | |
} | |
data = data.toString(); | |
all[count] = data; | |
completed++; | |
if (completed == urls.length) publish(); | |
})); | |
}) | |
} | |
function publish(){ | |
if(completed < urls.length) return false; for(i in all){ | |
console.log(all[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment