Created
May 26, 2015 03:20
-
-
Save yhsiang/af0eaf44070a360857cf to your computer and use it in GitHub Desktop.
my answers of https://github.com/workshopper/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
// # 1 | |
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
// # 10 | |
var http = require("http"); | |
var fs = require("fs"); | |
var server = http.createServer(function(req, res) { | |
var rs = fs.createReadStream(process.argv[3]); | |
rs.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
// # 11 | |
var http = require("http"); | |
var server = http.createServer(function(req, res) { | |
var map = require('through2-map') | |
req.pipe(map(function (chunk) { | |
return chunk.toString().toUpperCase(); | |
})).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
// # 12 | |
var http = require("http"); | |
var server = http.createServer(function(req, res) { | |
var url = require("url").parse(req.url, true); | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
var iso = new Date(url.query.iso); | |
if(url.pathname === '/api/parsetime') { | |
res.write(JSON.stringify({ | |
hour: iso.getHours(), | |
minute: iso.getMinutes(), | |
second: iso.getSeconds() | |
})); | |
} | |
if(url.pathname === '/api/unixtime') { | |
res.write(JSON.stringify({unixtime: iso.getTime()})); | |
} | |
res.end(); | |
}); | |
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
// # 2 | |
var sum = 0; | |
process.argv.shift(); | |
process.argv.shift(); | |
process.argv.map(function(i) { | |
sum += Number(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
// # 3 | |
var fs = require("fs"); | |
var content = fs.readFileSync(process.argv[2]).toString(); | |
console.log(content.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
// # 4 | |
var fs = require("fs"); | |
fs.readFile(process.argv[2], function(err, buf) { | |
console.log(buf.toString().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
// # 5 | |
var fs = require("fs"); | |
var ext = new RegExp("." + process.argv[3] + "$"); | |
fs.readdir(process.argv[2], function(err, list) { | |
list.map(function(item) { | |
if(ext.test(item)) console.log(item); | |
}); | |
}); |
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
// # 6 | |
var fs = require("fs"); | |
var path = require("path"); | |
module.exports = function (dir, ext, callback) { | |
fs.readdir(dir, function(err, list) { | |
if(err) return callback(err); | |
var data = list.filter(function(item) { | |
if(path.extname(item) === "." + ext) return true; | |
else return false; | |
}); | |
return callback(null, data); | |
}); | |
}; | |
var mymodule = require('./mymodule.js'); | |
mymodule(process.argv[2], process.argv[3], function(err, list) { | |
list.map(function(it) { | |
console.log(it); | |
}); | |
}); |
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
// # 7 | |
var http = require("http"); | |
http.get(process.argv[2], function(res) { | |
res.on('data', function(data) { | |
console.log(data.toString()); | |
}); | |
}); |
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
// # 8 | |
var http = require("http"); | |
var concatStream = require("concat-stream"); | |
function fetch(url, callback) { | |
var data = ''; | |
http.get(url, function(res) { | |
res.pipe(concatStream(function(d) { | |
data += d; | |
})); | |
res.on('end', function() { | |
callback(null,data); | |
}); | |
}); | |
} | |
fetch(process.argv[2], function(err, d1){ | |
console.log(d1); | |
fetch(process.argv[3], function(err, d2) { | |
console.log(d2); | |
fetch(process.argv[4], function(err, d3) { | |
console.log(d3); | |
}); | |
}); | |
}); |
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
// # 9 | |
var net =require("net"); | |
var server = net.createServer(function(socket) { | |
var date = new Date(); | |
var year = date.getFullYear() | |
, month = date.getMonth() + 1 | |
, day = date.getDate() | |
, hour = date.getHours() | |
, min = date.getMinutes(); | |
socket.end(year + "-" + ("0"+month).slice(-2)+ "-"+ ("0"+day).slice(-2) + " " + ("0"+hour).slice(-2) + ":" + min); | |
}); | |
server.listen(process.argv[2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment