Skip to content

Instantly share code, notes, and snippets.

@scottdomes
Last active April 11, 2016 21:52
Show Gist options
  • Save scottdomes/ef8b382e0eecda942696841e84037729 to your computer and use it in GitHub Desktop.
Save scottdomes/ef8b382e0eecda942696841e84037729 to your computer and use it in GitHub Desktop.
var net = require('net');
var server = net.createServer(function(socket) {
var date = printTime();
socket.end(date);
});
function zeroFill(i) {
return (i < 10 ? '0' : '') + i;
}
function printTime() {
var date = new Date();
return date.getFullYear() +
'-' + zeroFill(date.getMonth() + 1) +
'-' + date.getDate() +
' ' + date.getHours() +
':' + date.getMinutes() + '\n';
}
server.listen(Number(process.argv[2]));
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
var stream = fs.createReadStream(process.argv[3]);
stream.on('open', function() {
stream.pipe(res);
});
});
server.listen(process.argv[2]);
var http = require('http');
var fs = require('fs');
var map = require('through2-map');
var server = http.createServer(function(req, res) {
req.pipe(map(function (chunk) {
return chunk.toString().toUpperCase();
})).pipe(res);
});
server.listen(process.argv[2]);
var http = require('http');
var fs = require('fs');
var url = require('url');
var server = http.createServer(function(req, res) {
reqUrl = url.parse(req.url, true);
var date = new Date(reqUrl.query.iso);
res.writeHead(200, { 'Content-Type': 'application/json' });
if (reqUrl.pathname === '/api/parsetime') {
var time = {
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
};
res.end(JSON.stringify(time));
} else {
var time = {
unixtime: date.getTime()
};
res.end(JSON.stringify(time));
}
});
server.listen(process.argv[2]);
var http = require('http');
http.get(process.argv[2], function(res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function(new_data) {
data += new_data;
});
res.on('end', function() {
console.log(data.length);
console.log(data);
});
});
var http = require('http');
var data = [];
function getData(urlNumber) {
if (urlNumber < (process.argv.length)) {
http.get(process.argv[urlNumber], function(res) {
var dataString = '';
res.setEncoding('utf8');
res.on('data', function(new_data) {
dataString += new_data;
});
res.on('end', function() {
data.push(dataString);
getData(urlNumber + 1);
});
});
} else {
for (var i = 0; i < data.length; i++) {
console.log(data[i]);
}
}
}
getData(2);
var module = require('./module');
var http = require('http');
function sumNumbers(argv) {
var sum = 0;
for (var i = 2; i < argv.length; i++) {
sum += +argv[i];
}
console.log(sum);
}
function countNewlines(filepath) {
fs.readFile(filepath, function(err, data) {
if (!err) {
console.log(data.toString().split('\n').length - 1);
}
});
}
function readDirectories(filepath, ext) {
fs.readdir(filepath, function(err, list) {
for (var i = 0; i < list.length; i++) {
if (list[i].match('.' + ext + '$')) {
console.log(list[i]);
}
}
});
}
function listFiles(err, list) {
if (err) {
console.log(err);
} else {
for (var i = 0; i < list.length; i++) {
console.log(list[i]);
}
}
}
// module(process.argv[2], process.argv[3], listFiles);
http.get(process.argv[2], function(res) {
res.setEncoding('utf8');
res.on('data', console.log);
});
var fs = require('fs');
module.exports = function readDirectories(filepath, ext, callback) {
fs.readdir(filepath, function(err, list) {
if (err) {
return callback(err);
}
files = list.filter(function(file) {
if (file.match('.' + ext + '$')){
return file;
}
});
callback(null, files);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment