Last active
August 29, 2015 13:56
-
-
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.
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 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); |
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'); | |
| 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); | |
| } | |
| }); | |
| }); |
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'); | |
| 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); | |
| }); | |
| }; |
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'); | |
| 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); | |
| }); |
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'); | |
| 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); |
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
| var http = require('http'); | |
| var url = process.argv[2]; | |
| http.get(url, function (res) { | |
| res.setEncoding('utf8'); | |
| res.on('data', 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
| 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); | |
| })); | |
| }); |
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 http = require('http'); | |
| var fs = require('fs'); | |
| http.createServer(function (req, res) { | |
| fs.createReadStream(process.argv[3]).pipe(res); | |
| }).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
| 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]); |
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 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]); |
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 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); | |
| } |
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 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); | |
| }); | |
| }); |
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 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