Last active
December 24, 2015 03:59
-
-
Save jakl/6740506 to your computer and use it in GitHub Desktop.
nodeschool.io
Learn You The Node.js For Much Win!
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
node_modules/ |
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
require('fs').readFile(process.argv[2], 'utf8', function(e, data){ | |
console.log(data.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
var numbers = process.argv.slice(2) | |
var sum = 0 | |
for(var i = 0; i < numbers.length; i++) { | |
debugger; | |
sum += Number(numbers[i]) | |
} | |
console.log(sum) |
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
fs = require('fs') | |
require('http').createServer(function(req, res){ | |
fs.createReadStream(process.argv[2]).pipe(res) | |
}).listen(8000) |
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
fs = require('fs') | |
fs.readdir(process.argv[2], function(e, list){ | |
for(var i = 0; i < list.length; i++){ | |
file = list[i] | |
extension = file.slice(file.lastIndexOf('.') + 1) | |
debugger; | |
if(extension == process.argv[3]) | |
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
var fs = require('fs') | |
console.log(fs.readFileSync(process.argv[2]).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
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
colors = require('colors') | |
colors.mode='none' | |
http = require('http') | |
urls = process.argv.slice(2) | |
returned = [] // parallel array tracking stream 'end' events | |
urls.forEach(function(){returned.push(false)}) | |
urls.forEach(function(url, i){ | |
http.get(url, function(stream){ | |
stream.setEncoding('utf8') | |
urls[i] = '' // reuse rather than garbage collect this array #performance | |
stream.on('data', function(data){ | |
urls[i] += data | |
}) | |
stream.on('end', function(){ | |
returned[i] = true | |
if(returned.reduce(function(a,b){return a && b})) | |
urls.forEach(function(res){console.log(colors.green(res))}) | |
}) | |
stream.on('error', function(e){console.error(colors.red(e))}) | |
}) | |
}) |
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
require('http').get(process.argv[2], function(stream){ | |
stream.setEncoding('utf8') | |
stream.on('data', console.log) | |
stream.on('error', console.log) | |
}) |
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
require('http').get(process.argv[2], function(stream){ | |
stream.setEncoding('utf8') | |
totalData = '' | |
stream.on('data', function(data){totalData += data}) | |
stream.on('error', console.error) | |
stream.on('end', function(){console.log(totalData.length + '\n' + totalData)}) | |
}) |
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
map = require('through2-map') | |
require('http').createServer(function(req, res){ | |
req.pipe(map(function(chunk){ | |
return chunk.toString().toUpperCase() | |
})).pipe(res) | |
}).listen(8000) |
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
moment = require('moment') | |
url = require('url') | |
require('http').createServer(function(req, res){ | |
req_url = url.parse(req.url, true) | |
path = req_url.pathname | |
if(!(time = getTimeOrError(req_url.query.iso, res))) return | |
res.writeHead(200, { 'Content-Type': 'application/json' }) | |
if(path == '/api/parsetime') | |
res.end(JSON.stringify({ | |
hour: Number(time.format('h')), | |
minute: Number(time.format('m')), | |
second: Number(time.format('s')) | |
})) | |
if(path == '/api/unixtime') | |
res.end(JSON.stringify({ unixtime: time.valueOf() })) | |
res.end('invalid url, try /api/parsetime or /api/unixtime') | |
}).listen(8000) | |
function getTimeOrError(iso, res){ | |
if(!iso) { | |
res.end('invalid query params, try iso=somedate') | |
return | |
} | |
time = moment(req_url.query.iso) | |
if(!time.isValid()) { | |
res.end('invalid time, try 2013-07-04') | |
return | |
} | |
return time | |
} |
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
require('./modular.lib.js')(process.argv[2], process.argv[3], | |
function(e, valid_files){ | |
if(e) console.log(e) | |
else valid_files.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
module.exports = function(dir, valid_extension, cb){ | |
require('fs').readdir(dir, function(e, files){ | |
valid_files = [] | |
if(e) return cb(e) | |
for(var i=0; i < files.length; i++){ | |
var extension_index = files[i].lastIndexOf('.') + 1 | |
if(!extension_index) continue | |
if(valid_extension == files[i].slice(extension_index)) | |
valid_files.push(files[i]) | |
} | |
cb(null, valid_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
(function() { | |
'use strict'; | |
var http = require('http'), | |
through = require('through'), | |
port = process.argv[2] || 8000; | |
http.createServer(function(req, res) { | |
if(req.method === 'POST') { | |
req.pipe(through(function write(buffer) { | |
this.queue(buffer.toString().toUpperCase()); | |
})).pipe(res); | |
} | |
else { | |
res.end('Only accept POST method'); | |
} | |
}).listen(port, function() { | |
console.log('server listen on port '+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
{ | |
"name": "jakl-nodeschool", | |
"version": "1.0.0", | |
"description": "jakl keeping abreast of nodejs conventions and culture", | |
"repository": "https://gist.github.com/6740506.git", | |
"dependencies": { | |
"colors": "*", | |
"moment": "*", | |
"through": "*", | |
"through2-map": "*" | |
} | |
} |
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
moment = require('moment') | |
require('net').createServer(function(socket){ | |
socket.end(moment().format('YYYY-MM-DD HH:mm') + '\n') | |
}).listen(8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment