Created
January 14, 2017 15:50
-
-
Save pzi/afd63aff456f8e0745fcac5696be1ef5 to your computer and use it in GitHub Desktop.
my learnyounode solutions
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
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
let args = process.argv.slice(2); | |
let sum = 0; | |
for (var i = args.length - 1; i >= 0; i--) { | |
sum += +args[i]; | |
} | |
console.log(sum); | |
// var result = 0 | |
// for (var i = 2; i < process.argv.length; i++) { | |
// result += Number(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
const fs = require('fs'); | |
const path = process.argv[2]; | |
const file = fs.readFileSync(path); | |
const fileContent = file.toString(); | |
console.log(fileContent.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
const fs = require('fs'); | |
const path = process.argv[2]; | |
fs.readFile(path, 'utf8', (err, data) => { | |
if (err) return console.log(err); | |
const lines = data.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
const fs = require('fs'); | |
const path = require('path'); | |
const pathArg = process.argv[2]; | |
const extArg = process.argv[3]; | |
fs.readdir(pathArg, (err, files) => { | |
if (err) return console.log(err); | |
files.forEach( function(file) { | |
if (path.extname(file).includes(extArg)) { | |
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
const pathArg = process.argv[2]; | |
const extArg = process.argv[3]; | |
const filter = require('./06_modular_2.js'); | |
filter(pathArg, extArg, (err, filteredFiles) => { | |
if (err) return console.error(err); | |
filteredFiles.forEach((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
const fs = require('fs'); | |
const path = require('path'); | |
module.exports = function (dir, ext, callback) { | |
fs.readdir(dir, (err, files) => { | |
if (err) return callback(err); | |
files = files.filter((file) => { | |
return path.extname(file).includes(ext); | |
}); | |
callback(null, files); | |
}); | |
} |
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
const http = require('http'); | |
const url = process.argv[2]; | |
http.get(url, (response) => { | |
response.setEncoding('utf8'); | |
// verbose callback | |
response.on('data', (data) => { console.log(data); }); | |
// simple callback | |
response.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
const http = require('http'); | |
const url = process.argv[2]; | |
http.get(url, (response) => { | |
response.setEncoding('utf8'); | |
let rawData = ''; | |
response.on('error', console.error); | |
response.on('data', (chunk) => { | |
rawData += chunk; | |
}); | |
response.on('end', (data) => { | |
console.log(rawData.length); | |
console.log(rawData); | |
}); | |
}); |
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
const http = require('http'); | |
const args = process.argv.slice(2); | |
function collectData (url, callback) { | |
http.get(url, (response) => { | |
response.setEncoding('utf8'); | |
let rawData = ''; | |
response.on('error', (error) => callback(error)); | |
response.on('data', (chunk) => { | |
rawData += chunk; | |
}); | |
response.on('end', (data) => { | |
callback(null, rawData); | |
}); | |
}); | |
} | |
var argCount = args.length; | |
let allData = {}; | |
args.forEach((url, index) => { | |
collectData(url, (err, data) => { | |
allData[index] = data; | |
argCount--; | |
if (argCount <= 0) { | |
Object.keys(allData).map(key => console.log(allData[key])) | |
} | |
}); | |
}); |
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
const net = require('net'); | |
const port = process.argv[2]; | |
function zeroFill(i) { | |
return i < 10 ? '0' + i : i; | |
} | |
function now() { | |
const date = new Date(); | |
return date.getFullYear() + '-' + | |
zeroFill(date.getMonth() + 1) + '-' + | |
zeroFill(date.getDate()) + ' ' + | |
zeroFill(date.getHours()) + ':' + | |
zeroFill(date.getMinutes()); | |
} | |
const server = net.createServer((connection) => { | |
connection.end(now() + '\n'); | |
}); | |
server.on('error', (err) => { | |
throw err; | |
}); | |
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
const http = require('http'); | |
const fs = require('fs'); | |
const port = process.argv[2]; | |
const path = process.argv[3]; | |
const server = http.createServer((request, response) => { | |
response.writeHead(200, {'content-type': 'text/plain'}); | |
fs.createReadStream(path).pipe(response); | |
}); | |
server.on('error', (err) => { | |
throw err; | |
}); | |
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
const http = require('http'); | |
const map = require('through2-map'); | |
const port = process.argv[2]; | |
const server = http.createServer((request, response) => { | |
if (request.method !== 'POST') return response.end('Request is not POST!\n'); | |
request.pipe(map((chunk) => { | |
return chunk.toString().toUpperCase(); | |
})).pipe(response); | |
}); | |
server.on('error', (err) => { | |
throw err; | |
}); | |
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
const http = require('http'); | |
const url = require('url'); | |
const port = process.argv[2]; | |
function parseTime(time) { | |
return { | |
'hour': time.getHours(), | |
'minute': time.getMinutes(), | |
'second': time.getSeconds() | |
} | |
} | |
function unixTime(time) { | |
return { | |
'unixtime': time.getTime() | |
} | |
} | |
const server = http.createServer((request, response) => { | |
if (request.method !== 'GET') return response.end('Request is not GET!\n'); | |
const URLobj = url.parse(request.url, true); | |
const time = new Date(URLobj.query.iso); | |
let result; | |
if (URLobj.pathname === '/api/parsetime') { | |
result = parseTime(time); | |
} | |
if (URLobj.pathname === '/api/unixtime') { | |
result = unixTime(time); | |
} | |
response.writeHead(200, {'content-type': 'application/json'}); | |
response.end(JSON.stringify(result)); | |
}); | |
server.on('error', (err) => { | |
throw err; | |
}); | |
server.listen(+port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment