Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonkemp/9144683 to your computer and use it in GitHub Desktop.
Save jonkemp/9144683 to your computer and use it in GitHub Desktop.
Here's my passing scripts for running learnyounode https://github.com/rvagg/learnyounode#learn-you-the-nodejs-for-much-win.
var args = process.argv.slice(2);
var total = 0;
for (var i = 0, len = args.length; i < len; i++) {
total += Number(args[i]);
}
console.log(total);
var fs = require('fs');
var path = require('path');
var args = process.argv.slice(2);
var dirpath = args[0];
var ext = args[1];
fs.readdir(dirpath, function (err, list) {
list.filter(function (name) {
var regex = new RegExp(ext);
if (regex.test(path.extname(name))) {
console.log(name);
}
});
});
var fs = require('fs');
var path = require('path');
module.exports = function (dirpath, ext, cb) {
fs.readdir(dirpath, function (err, list) {
var files = [];
if (err) {
return cb(err);
}
list.filter(function (name) {
var regex = new RegExp(ext);
if (regex.test(path.extname(name))) {
files.push(name);
}
});
cb(null, files);
});
};
var fs = require('fs');
var args = process.argv.slice(2);
var path = args[0];
fs.readFile(path, function (err, data) {
var newlines = data.toString();
newlines = newlines.split('\n');
console.log(newlines.length - 1);
});
var fs = require('fs');
var args = process.argv.slice(2);
var path = args[0];
var fileContents = fs.readFileSync(path);
var newlines = fileContents.toString();
newlines = newlines.split('\n');
console.log(newlines.length - 1);
console.log('HELLO WORLD');
var http = require('http');
var url = process.argv[2];
http.get(url, function (res) {
res.setEncoding('utf8');
res.on('data', console.log);
});
var http = require('http');
var bl = require('bl');
var url = process.argv[2];
http.get(url, function (res) {
res.pipe(bl(function (err, data) {
data = data.toString();
console.log(data.length);
console.log(data);
}));
});
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.createReadStream(process.argv[3]).pipe(res);
}).listen(process.argv[2]);
var http = require('http');
var url = require('url');
http.createServer(function (req, res) {
var output;
var obj = url.parse(req.url, true);
var date = new Date(obj.query.iso);
function unixtime(time) {
return { "unixtime": time.getTime() };
}
function parsetime(time) {
return { "hour": time.getHours(), "minute": time.getMinutes(), "second": time.getSeconds() };
}
if (obj.pathname === '/api/unixtime') {
output = unixtime(date);
}
if (obj.pathname === '/api/parsetime') {
output = parsetime(date);
}
if (output) {
res.writeHead(200, { 'Content-Type': 'application/json'});
res.end(JSON.stringify(output));
} else {
res.writeHead(404);
res.end();
}
}).listen(process.argv[2]);
var http = require('http');
var map = require('through2-map');
http.createServer(function (req, res) {
if (req.method !== 'POST') {
return res.end('Invalid request format.\n');
}
req.pipe(map(function (chunk) { return chunk.toString().toUpperCase(); }))
.pipe(res);
}).listen(process.argv[2]);
var http = require('http');
var bl = require('bl');
var args = process.argv.slice(2);
var arr = ['', '', ''];
function responseReady(arr) {
if (arr.every(function (item) { return item !== ''; }) ) {
arr.forEach(function (item) {
console.log(item);
});
}
}
function httpGet(i) {
http.get(args[i], function (res) {
res.pipe(bl(function (err, data) {
data = data.toString();
arr[i] = data;
responseReady(arr);
}));
});
}
for (var i=0; i < 3; i++) {
httpGet(i);
}
var filterFiles = require('./filterFiles');
var args = process.argv.slice(2);
var dirpath = args[0];
var ext = args[1];
filterFiles(dirpath, ext, function (err, data) {
data.forEach(function (name) {
console.log(name);
});
});
var net = require('net');
function pad(num) {
return (num < 10 ? '0' : '') + num;
}
net.createServer(function (socket) {
var date = new Date();
var time = date.getFullYear() + '-' +
pad(date.getMonth() + 1) + '-' +
pad(date.getDate()) + ' ' +
pad(date.getHours()) + ':' +
pad(date.getMinutes());
socket.end(time + '\n');
}).listen(process.argv[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment