diff --git a/baby-steps.js b/baby-steps.js new file mode 100644 index 0000000..c60f7fe --- /dev/null +++ b/baby-steps.js @@ -0,0 +1 @@ +console.log(process.argv.slice(2).reduce((a,b) => Number(a) + Number(b))) \ No newline at end of file diff --git a/filtered-ls.js b/filtered-ls.js new file mode 100644 index 0000000..52bd9e7 --- /dev/null +++ b/filtered-ls.js @@ -0,0 +1,15 @@ +const fs = require('fs') + +const dirName = process.argv[2] +const fileExtension = process.argv[3] + +fs.readdir(dirName, + function callback (err, list) { + if (err) throw err + + const re = new RegExp("\." + fileExtension + "$") + const filteredList = list.filter((item) => re.test(item)) + + console.log(filteredList.join('\n')) + }) + \ No newline at end of file diff --git a/hello-world.js b/hello-world.js new file mode 100644 index 0000000..5adb4f5 --- /dev/null +++ b/hello-world.js @@ -0,0 +1 @@ +console.log('HELLO WORLD') \ No newline at end of file diff --git a/http-client.js b/http-client.js new file mode 100644 index 0000000..46a515d --- /dev/null +++ b/http-client.js @@ -0,0 +1,8 @@ +const http = require('http') + +http.get(process.argv[2], (response) => { + response.setEncoding('utf8') + response.on('data', (data) => { + console.log(data) + }) + }) \ No newline at end of file diff --git a/http-collect.js b/http-collect.js new file mode 100644 index 0000000..8075efd --- /dev/null +++ b/http-collect.js @@ -0,0 +1,13 @@ +const http = require('http') +let fullResult = '' + +http.get(process.argv[2], (response) => { + response.setEncoding('utf8') + response.on('data', (data) => { + fullResult += data + }) + response.on('end', () => { + console.log(fullResult.length) + console.log(fullResult) + }) + }) \ No newline at end of file diff --git a/http-file-server.js b/http-file-server.js new file mode 100644 index 0000000..e2f5ed9 --- /dev/null +++ b/http-file-server.js @@ -0,0 +1,17 @@ +const http = require('http') +const fs = require('fs') +const port = process.argv[2] +const filePath = process.argv[3] + +const server = http.createServer(function listener(req, res) { + const stream = fs.createReadStream(filePath) + let text = "" + stream.on('data', (chunk) => { + text += chunk + }) + stream.on('end', () => { + res.end(text) + }) +}) + +server.listen(port) \ No newline at end of file diff --git a/http-json-api-server.js b/http-json-api-server.js new file mode 100644 index 0000000..16abbd5 --- /dev/null +++ b/http-json-api-server.js @@ -0,0 +1,25 @@ +const http = require('http') +const port = process.argv[2] + +const server = http.createServer(function listener(req, res) { + res.writeHead(200, { 'Content-Type': 'application/json' }) + + const path = req.url.split("?")[0] + const value = req.url.split("=")[1] + + const date = new Date(value) + + if (path == "/api/parsetime") + res.end(JSON.stringify({hour: date.getHours(), + minute: date.getMinutes(), + second: date.getSeconds() + })) + else if (path == "/api/unixtime") + res.end(JSON.stringify({unixtime: date.getTime()})) + else { + res.writeHead(404) + res.end() + } +}) + +server.listen(port) \ No newline at end of file diff --git a/http-uppercaser.js b/http-uppercaser.js new file mode 100644 index 0000000..6438dda --- /dev/null +++ b/http-uppercaser.js @@ -0,0 +1,16 @@ +const http = require('http') +const fs = require('fs') +const port = process.argv[2] +const filePath = process.argv[3] + +const server = http.createServer(function listener(req, res) { + let output = "" + req.on('data', (chunk) => { + output += String(chunk).toUpperCase() + }) + req.on('end', () => { + res.end(output) + }) +}) + +server.listen(port) \ No newline at end of file diff --git a/juggling-async.js b/juggling-async.js new file mode 100644 index 0000000..f980c4c --- /dev/null +++ b/juggling-async.js @@ -0,0 +1,26 @@ +const http = require('http') +let fullData = ["", "", ""] +let done = [false, false, false] + +function finish() { + console.log(fullData[0]) + console.log(fullData[1]) + console.log(fullData[2]) +} + +function callback(response,id) { + response.setEncoding('utf8') + response.on('data', (data) => { + fullData[id] += data + }) + response.on('end', () => { + done[id] = true + if (done.every(d => d)) { + finish() + } + }) +} + +http.get(process.argv[2], (response) => { callback(response,0) }) +http.get(process.argv[3], (response) => { callback(response,1) }) +http.get(process.argv[4], (response) => { callback(response,2) }) \ No newline at end of file diff --git a/make-it-modular.js b/make-it-modular.js new file mode 100644 index 0000000..b6d9f9d --- /dev/null +++ b/make-it-modular.js @@ -0,0 +1,8 @@ +const mymodule = require('./mymodule') +const fileExtension = process.argv[3] + +mymodule(process.argv[2], process.argv[3], + function callback(err, data) { + console.log(data.join('\n')) + } +) \ No newline at end of file diff --git a/my-first-async-io.js b/my-first-async-io.js new file mode 100644 index 0000000..63e5470 --- /dev/null +++ b/my-first-async-io.js @@ -0,0 +1,6 @@ +const fs = require('fs') +fs.readFile(process.argv[2],'utf8', + function callback (err,data) { + if (err) throw err + console.log(data.match(/\n/g).length) +}) \ No newline at end of file diff --git a/my-first-io.js b/my-first-io.js new file mode 100644 index 0000000..61aeba3 --- /dev/null +++ b/my-first-io.js @@ -0,0 +1,3 @@ +const fs = require('fs') +const file = fs.readFileSync(process.argv[2],'utf8') +console.log(file.match(/\n/g).length) \ No newline at end of file diff --git a/mymodule.js b/mymodule.js new file mode 100644 index 0000000..1c4d830 --- /dev/null +++ b/mymodule.js @@ -0,0 +1,12 @@ +module.exports = function (dirName, fileExtension, callback) { + + const fs = require('fs') + + fs.readdir(dirName, function (err, data) { + if (err) return callback(err) + + const re = new RegExp("\." + fileExtension + "$") + callback(err, data.filter((item) => re.test(item))) + + }) +} \ No newline at end of file diff --git a/time-server.js b/time-server.js new file mode 100644 index 0000000..541c3b7 --- /dev/null +++ b/time-server.js @@ -0,0 +1,15 @@ +const net = require('net') +const port = process.argv[2] + +const server = net.createServer(function listener(socket) { + const date = new Date() + const dateString = date.getFullYear() + "-" + + (date.getMonth()+1).toString().padStart(2,'0') + "-" + + date.getDate().toString().padStart(2,'0') + " " + + date.getHours().toString().padStart(2,'0') + ":" + + date.getMinutes().toString().padStart(2,'0') + "\n" + + socket.end(dateString) +}) + +server.listen(port) \ No newline at end of file