Last active
June 21, 2019 04:09
-
-
Save khriztianmoreno/b98d938bef420d5688908988d6a23402 to your computer and use it in GitHub Desktop.
Nodeschool.io - learnyounode
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
console.log("HELLO WORLD"); |
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
const http = require('http') | |
const fs = require('fs') | |
const filename = process.argv[3] | |
const server = http.createServer(function (req, res) { | |
res.writeHead(200, { 'content-type': 'text/plain' }) | |
fs.createReadStream(filename).pipe(res); | |
}) | |
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
const http = require('http'); | |
const map = require('through2-map'); | |
const uc = map((chunk) => { | |
return chunk.toString().toUpperCase(); | |
}); | |
const server = http.createServer((req, res) => { | |
if (req.method !== 'POST') { | |
return res.end('send me a POST\n') | |
} | |
req.pipe(uc).pipe(res); | |
}); | |
server.listen(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
const http = require('http') | |
const url = require('url') | |
function parsetime (time) { | |
return { | |
hour: time.getHours(), | |
minute: time.getMinutes(), | |
second: time.getSeconds() | |
} | |
} | |
function unixtime (time) { | |
return { unixtime: time.getTime() } | |
} | |
const server = http.createServer((req, res) => { | |
const parsedUrl = url.parse(req.url, true) | |
const time = new Date(parsedUrl.query.iso) | |
let 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 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 sum = 0; | |
for(let i = 2; i < process.argv.length; i++){ | |
sum = sum + Number(process.argv[i]); | |
} | |
console.log(sum); |
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
const fs = require('fs'); | |
const str = fs.readFileSync(process.argv[2]).toString(); | |
console.log(str.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
const fs = require('fs'); | |
const file = process.argv[2]; | |
fs.readFile(file, (err, contents) => { | |
// fs.readFile(file, 'utf8', callback) can also be used | |
if (err) { | |
return console.log(err) | |
} | |
const lines = contents.toString().split('\n').length - 1 | |
console.log(lines); | |
}); |
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
const fs = require('fs') | |
const path = require('path') | |
const folder = process.argv[2] | |
const ext = `.${process.argv[3]}` | |
fs.readdir(folder, (err, files) => { | |
if (err) return console.error(err) | |
files.forEach((file) => { | |
if (path.extname(file) === ext) { | |
console.log(file) | |
} | |
}) | |
}) |
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
const http = require('http') | |
const url = process.argv[2] | |
http.get(url, (response) => { | |
response.setEncoding('utf8') | |
response.on('data', console.log) | |
response.on('error', console.error) | |
}).on('error', console.error) |
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
const http = require('http') | |
const bl = require('bl') | |
const bufferList = bl((err, data) => { | |
if (err) { | |
return console.error(err) | |
} | |
data = data.toString() | |
console.log(data.length) | |
console.log(data) | |
}) | |
http.get(process.argv[2], (response) => { | |
response.pipe(bufferList) | |
}) |
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
const http = require('http') | |
const bl = require('bl') | |
let results = [] | |
let count = 0 | |
function printResults () { | |
for (let i = 0; i < 3; i++) { | |
console.log(results[i]) | |
} | |
} | |
function httpGet(index) { | |
http.get(process.argv[2 + index], (response) => { | |
response.pipe(bl((err, data) => { | |
if (err) { | |
return console.error(err) | |
} | |
results[index] = data.toString() | |
count++ | |
if (count === 3) { | |
printResults() | |
} | |
})) | |
}) | |
} | |
for (var i = 0; i < 3; i++) { | |
httpGet(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
const net = require('net') | |
function zeroFill (i) { | |
return ( | |
i < 10 ? '0' : '' | |
) + i | |
} | |
function now () { | |
const d = new Date() | |
return d.getFullYear() + '-' + | |
zeroFill(d.getMonth() + 1) + '-' + | |
zeroFill(d.getDate()) + ' ' + | |
zeroFill(d.getHours()) + ':' + | |
zeroFill(d.getMinutes()) | |
} | |
const server = net.createServer(function (socket) { | |
socket.end(now() + '\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